如何在 google cloudbuild 中跨连续构建步骤(包括 docker 步骤)访问内容

how to access content across successive build steps (including in docker steps) in google cloudbuild

在 Google Cloud Platform 中,在多阶段云构建过程中,可以使用共享 /workspace 从一个构建步骤到下一个构建步骤访问内容。 例如,以下 cloudbuild.yaml 将打印“hello world”,其中从第一个构建步骤访问文本 hello。

steps:
  - name: gcr.io/cloud-builders/gcloud
    entrypoint: 'bash'
    args: [ '-c', " echo world > sample.txt" ]
  - name: gcr.io/cloud-builders/gcloud
    entrypoint: 'bash'
    args: [ '-c', "echo hello $( < sample.txt)" ]

但是,在 docker 构建步骤中访问此内容时,此方法似乎不起作用。 docker 构建步骤中似乎没有访问文件的方法。

以下步骤作为上述步骤之后的第三步执行时会执行 Dockerfile,但似乎不会使 sample.txt 文件可用于其中的命令。

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    args: ['build',
           '-t', 'repositoryname:latest',
           '.']

我试过以下方法:

在cloudbuild中是否可以通过包括docker步在内的一系列步骤逐步构建内容?如果是这样,如何做到这一点?

更新

实际上,文件似乎确实保留在工作区环境中。我的问题是无法在构建命令中使用文件的内容,如下所示。

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    args: ['build',
           '--build-arg', 'GITHUB_AUTH_TOKEN=$( < sample.txt)',
           '-t', '$_REPO_PATH/$_PROJECT_ID/$_REPO_NAME:$_TAG_NAME',
           '.']

在第一步中,我实际上是使用 gcloud 从秘密管理器获取秘密,然后我想将其作为参数传递给 docker 构建过程。 我无法使上述构建步骤起作用,然后我读到命令替换对 docker 参数不起作用?

问题来自args解释。像这样写,没有任何 arg 评估。像这样使用脚本模式

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    entrypoint: "bash"
    args: 
      - "-c"
      - |
          docker build --build-arg GITHUB_AUTH_TOKEN=$( < sample.txt) -t $_REPO_PATH/$_PROJECT_ID/$_REPO_NAME:$_TAG_NAME .