如何通过 Google Cloud Build 中的一个步骤设置环境或替换变量? [英] How do I set an environment or substitution variable via a step in Google Cloud Build?

查看:19
本文介绍了如何通过 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 抱怨导出",并且即使有效,它也可能是不同的环境:

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`']

然后,在后面的步骤中:

Then, in a later step:

  # 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:

在早期,我生成了基于日期的标记并将其写入文件.文件系统 (/workspace) 在步骤之间保留,并用作我的环境变量的存储.然后,在我需要引用该值的每个步骤中,我都会将该文件放在适当的位置.诀窍是在每个容器中使用 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 调用永远不会被单引号包围, 只有 double,这可能需要转义内部双引号以构建 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'

这篇关于如何通过 Google Cloud Build 中的一个步骤设置环境或替换变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆