如何通过Google Cloud Build中的步骤设置环境或替换变量?

如何通过Google Cloud Build中的步骤设置环境或替换变量?

问题描述:

基本上,在使用Google Cloud Build时,如何读取在后续步骤中在先前的构建步骤中写入的值?

Basically, when using Google Cloud Build, how do I read a value that was written in an earlier build step in subsequent steps?

具体来说,我想制作一个基于时间戳和$ SHORT_SHA组合的自定义图像标签.如下所示.但是,它不起作用,因为docker抱怨导出",即使起作用,它也可能是一个不同的env:

Specifically, I'd like to make a custom image tag that's based on a combination of the timestamp and $SHORT_SHA. Something like the below. Though, it doesn't work, as docker complains about "export", and, even if that worked, it likely will be a different env:

  # Setting tag in a variable:
  - name: 'ubuntu'
    args: ['export', '_BUILD_TAG=`date', '-u', '+%Y%m%dT%H%M%S_$SHORT_SHA`']

然后,在后续步骤中:

  # Using tag from the variable:
  - name: gcr.io/cloud-builders/docker
    args: ['build', '-t', 'gcr.io/$PROJECT_ID/$_BUILD_TAG', '.']

那么,如何使用一个步骤的输出到另一个步骤?我可以将date的内容写到文件中,然后读取它,但是我回到不知道如何从读取的文件中设置变量(或者以其他方式插值其结果以构成docker build的参数) .

So, how do I use the output of one step in another? I could write the contents of date to a file, and then read it, but I'm back at not knowing how to set the variable from the file I read (or otherwise interpolate its results to form the argument to docker build).

我从未找到一种在一个构建步骤中设置环境变量的方法,该方法可以在其他步骤中读取,但是我最终通过在康斯坦丁的回答如下:

I never found a way to set an environment variable in one build step that can be read in other steps, but I ended up accomplishing the same effect by building on Konstantin's answer in the following way:

在早期的步骤中,我生成基于​​日期的标记并将其写入文件.文件系统(/工作区)在步骤之间保留,并用作环境变量的存储.然后,在需要引用该值的每个步骤中,我都会将该文件放在适当的位置.诀窍是使用sh或bash作为每个容器中的入口点,以便可以执行从文件读取的子shell.

In an early step, I generate and write my date-based tag to a file. The filesystem (/workspace) is retained between steps, and serves as store of my environment variable. Then, in each step that I need to reference that value, I cat that file in place. The trick is to use sh or bash as the entrypoint in each container so that the sub-shell that reads from the file can execute.

这是一个例子:

## Set build tag and write to file _TAG
- name: 'ubuntu'
  args: ['bash', '-c', 'date -u +%Y%m%dT%H%M_$SHORT_SHA > _TAG']

...

# Using the _TAG during Docker build:
- name: gcr.io/cloud-builders/docker
entrypoint: sh
args: ['-c', 'docker build -t gcr.io/$PROJECT_ID/image_name:$(cat _TAG) .']

需要注意的一点是,如果您是在JSON对象或需要双引号的内容中以这种方式进行bash插值,则在容器中执行子shell调用时,永远不要用单引号将其包围,只有双精度,这可能需要转义内部双引号以构建JSON对象.这是一个示例,其中我使用_TAG文件值来修补kubernetes配置,以部署新生成的映像:

A caveat to note is that if you are doing the bash interpolation in this way within, say, a JSON object or something that requires double quotes, you need the subshell call to never be surrounded by single quotes when executed in the container, only double, which may require escaping the internal double quotes to build the JSON object. Here's an example where I patch the kubernetes config using the _TAG file value to deploy the newly-build image:

- name: gcr.io/cloud-builders/kubectl
entrypoint: bash
args: ['-c', 'gcloud container clusters get-credentials --zone $$CLOUDSDK_COMPUTE_ZONE $$CLOUDSDK_CONTAINER_CLUSTER ; kubectl patch deployment deployment_name -n mynamespace -p "{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"image_name\",\"image\":\"gcr.io/$PROJECT_ID/image_name:$(cat _TAG)\"}]}}}}}"']
env:
- 'CLOUDSDK_COMPUTE_ZONE=us-central1-b'
- 'CLOUDSDK_CONTAINER_CLUSTER=my-google-proj-cluster-name'