Python 将GitlabCI中的文件变量传递给Docker运行(FileNotFound)
我在GitlabCI中创建了一个受保护的文件变量,以便将名称设置为Python 将GitlabCI中的文件变量传递给Docker运行(FileNotFound),python,docker,google-cloud-platform,gitlab,gitlab-ci,Python,Docker,Google Cloud Platform,Gitlab,Gitlab Ci,我在GitlabCI中创建了一个受保护的文件变量,以便将名称设置为GOOGLE\u CREDENTIALS,该值是用作我的GOOGLE云服务帐户密钥的JSON文件的内容 根据文档,可以在任何命令中使用此文件变量,因此我使用以下命令: 这是my.gitlab ci.yml的内容: stages: - build_image image: docker:latest services: - docker:dind build_image: stage: build_image
GOOGLE\u CREDENTIALS
,该值是用作我的GOOGLE云服务帐户密钥的JSON文件的内容
根据文档,可以在任何命令中使用此文件变量,因此我使用以下命令:
这是my.gitlab ci.yml
的内容:
stages:
- build_image
image: docker:latest
services:
- docker:dind
build_image:
stage: build_image
script:
- docker build . -t johndoe/project_name
- docker run --volume ${PWD}:/src --env=GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_CREDENTIALS johndoe/project_name
我的Dockerfile(以防万一):
这是使用密钥的代码部分:
try:
key_path = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
except KeyError:
key_path = "./bigquery_key/key.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
client = bigquery.Client(
credentials=credentials,
project=credentials.project_id,
)
我得到的错误如下:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/builds/johndoe/project\u name.tmp/GOOGLE\u CREDENTIALS'
使用echo
和cat
显示文件确实存在,并且内容与JSON文件和密钥完全匹配,但是由于某种原因,Docker容器不知道它存在
我错过了什么
try:
key_path = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
except KeyError:
key_path = "./bigquery_key/key.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
client = bigquery.Client(
credentials=credentials,
project=credentials.project_id,
)