Google 云构建 - 为 docker 构建步骤提供资产

Google cloud build - provide assets to docker build step

我正在尝试合并 Google Cloud Build 文档

中的两个示例

This example where a container is built using a yaml file

And this example where a persistent volume is used

我的场景很简单,构建的第一步会生成一些资产,我想将其捆绑到后续步骤中构建的 Docker 图像中。但是,由于 COPY command is ,我无法确定如何在用于第二步的 Docker 文件中引用第一步中的资产。

可能有多种方法可以解决这个问题,但我发现最简单的方法是使用 docker cloud builder 和 运行 一个 sh 脚本(因为 Bash 未包含在 docker 图像中)。

在此示例中,我们基于问题中的示例进行构建,在第一步构建步骤中生成资产 file,然后在第二步的 Dockerfile 中使用它。

cloudbuild.yaml

steps:
- name: 'ubuntu'
  volumes:
  - name: 'vol1'
    path: '/persistent_volume'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
        echo "Hello, world!" > /persistent_volume/file
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'sh'
  volumes:
  - name: 'vol1'
    path: '/persistent_volume'
  args: [ 'docker-build.sh' ]
  env:
    - 'PROJECT=$PROJECT_ID'
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'

docker-build.sh

#/bin/sh
cp -R /persistent_volume .
docker build -t gcr.io/$PROJECT/quickstart-image .