【问题标题】:How to pass substitution variables on Google Cloud build to run a dockerfile如何在 Google Cloud 构建上传递替换变量以运行 dockerfile
【发布时间】:2020-10-28 07:13:25
【问题描述】:

我在 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进行消费。 这就是我在触发页面上填充替换变量的方式

粘贴第 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?

【问题讨论】:

  • 你能分享完整的错误构建代码吗?
  • @ThanhNguyenVan 我已经用出现错误的构建日志更新了这个问题。

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


【解决方案1】:

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

steps:
- name: 'gcr.io/cloud-builders/docker'
  env:
    - 'ACCESS_TOKEN=$_ACCESS_TOKEN'
  args:
    - build
    - "--build-arg=ACCESS_TOKEN=${ACCESS_TOKEN}"
    - "--tag=gcr.io/$PROJECT_ID/quickstart-image"
    - "--file=./twitter/dax/processing_scripts/Dockerfile"

另一种选择是在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 字段,这由您决定。

【讨论】:

  • 当我尝试您发布的第一个选项解决方案时,我收到以下错误:“未知标志:--build-arg ACCESS_TOKEN”。不知道为什么
  • 这可能是因为你需要包含一个=。我更新了答案。
  • @RangaVittal 您收到该错误的原因是因为它应该是 _ACCESS_TOKEN。所有替换变量都以和下划线开头。这 。最后 docker cmd 使用不正确。
猜你喜欢
  • 2021-05-19
  • 2019-09-28
  • 1970-01-01
  • 2019-11-26
  • 2021-07-10
  • 2020-01-05
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
相关资源
最近更新 更多