docker - 如何在Google Cloud构建上传递替代变量以运行dockerfile

标签 docker google-cloud-platform yaml dockerfile google-cloud-build

我已经在Google Cloud构建上设置了一个Github触发器。我已经有一个dockerfile来执行构建步骤。由于没有规定传递替代变量,因此我尝试使用云构建配置文件(yaml)进行构建,然后将路径传递至配置文件中的dockerfile。
这是云构建配置文件,我需要传递5个变量供Dockerfile使用,而yaml文件如下所示:

steps:
- name: 'gcr.io/cloud-builders/docker'
  env:
  - 'ACCESS_TOKEN=$ACCESS_TOKEN'
  - 'BRANCH=$BRANCH'
  - 'UTIL_BRANCH=$UTIL_BRANCH'
  - 'ARG_ENVIRONMENT=$ARG_ENVIRONMENT'
  - 'ARG_PYTHON_SCRIPT=$ARG_PYTHON_SCRIPT'
  args:
  - build
  - "--tag=gcr.io/$PROJECT_ID/quickstart-image"
  - "--file=./twitter/dax/processing_scripts/Dockerfile"
  - .
当触发器运行构建时,我在dockerfile中的一个构建步骤中收到一个错误,指出该变量不可用。显然,yaml文件中传递的环境变量不会传递给Dockerfile进行使用。
这就是我在触发页面上填写替换变量的方式
enter image description here
将步骤5中的生成代码粘贴到发生错误的位置,在此之前运行apt-get update命令:
Step 5/28 : RUN git config --global url."https://${ACCESS_TOKEN}:@github.com/".insteadOf "https://github.com/" && echo $(ACCESS_TOKEN)
 ---> Running in f7b94bc2a0d9

/bin/sh: 1: ACCESS_TOKEN: not found
Removing intermediate container f7b94bc2a0d9
 ---> 30965207dcec
Step 6/28 : ARG BRANCH
 ---> Running in 93e36589ac48
Removing intermediate container 93e36589ac48
 ---> 1d1508b1c1d9
Step 7/28 : RUN git clone https://github.com/my_repo45/twitter.git -b "${BRANCH}"
 ---> Running in fbeb93dbb113
Cloning into 'twitter'...
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/my_repo45/twitter.git/'
The command '/bin/sh -c git clone https://github.com/my_repo45/twitter.git -b "${BRANCH}"' returned a non-zero code: 128
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 128```

Could anyone please point out what the issue and validate if the environment declaration is proper?

最佳答案

您仍然需要将变量添加到docker build命令。与当前情况一样,这些变量仅在云构建中可供您使用,而不能作为常规环境变量使用。下面概述了如何将这些变量传递到云构建的简化示例。请注意,只能将其与dockerfile中的ARG一起使用,而不能与ENV一起使用。

steps:
- name: 'gcr.io/cloud-builders/docker'
  env:
    - 'ACCESS_TOKEN=$ACCESS_TOKEN'
  args:
    - build
    - "--tag=gcr.io/$PROJECT_ID/quickstart-image"
    - "--file=./twitter/dax/processing_scripts/Dockerfile"
    - "--build-arg=ACCESS_TOKEN=${ACCESS_TOKEN}"
    - .
另一个选择是在docker build命令所在的同一构建步骤中导出环境变量:
steps:
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  env:
    - 'ACCESS_TOKEN=$ACCESS_TOKEN'
  args:
  - '-c'
  - |
    export ACCESS_TOKEN=${ACCESS_TOKEN}
    docker build \
      --tag=gcr.io/$PROJECT_ID/quickstart-image" \
      --file=./twitter/dax/processing_scripts/Dockerfile" \
      .
当然,您可以争论是否在此设置中仍然需要env字段,我将由您自己决定。

关于docker - 如何在Google Cloud构建上传递替代变量以运行dockerfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64568013/

相关文章:

python - Google Cloud Function 无法安装 python 包

amazon-web-services - 如何使用 aws-cli 访问 Google Cloud Storage 存储桶

php - 生成的 Doctrine 模型尊重大小写,但生成的 Yaml 不

java - Bukkit 的无效 plugin.yml 异常

permissions - Docker:打开/certs/domain.crt:权限被拒绝

php - 加载代理模块时,Docker 中的 Apache 2.4 httpd 不会启动

Selenium Grid 与不同主机上的 Docker 容器

mysql - Logstash 无法连接到 MySQL 数据库

google-cloud-platform - Google Cloud 向项目添加用户

postgresql - 使用 docker swarm 模式时如何将数据保存在卷上?