steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/image_folder', '.']
timeout: 500s
images:
- gcr.io/$PROJECT_ID/image_folder
现在docker映像存储在Container Registry中,我设置了一个构建触发器来构建我们的程序。我们程序的框架将不断变化,因此至关重要的是,在进行测试之前,我们的管道必须定期重建程序。为此,我要参考存储在我们的Container Registry中的上一个图像,以将其作为Google Cloud上的自定义生成器运行。目前,自定义生成器的参数调用了一个Python脚本,该脚本使用pythonos.system
向系统提供命令,这些命令调用构建程序所需的步骤。此构建步骤的cloudbuild.yml文件存储在我们的Google Cloud Source Repository中,以便可以通过推送到我们的存储库来触发它。 cloudbuild.yml文件如下:
steps: - name: 'gcr.io/$PROJECT_ID/image_folder:latest' entrypoint: 'bash' args: - '-c' - 'python3 path/to/instructions/build_instructions.py' timeout: 2800s
下一步是创建另一个构建触发器,该触发器将使用上一步中构建的构建来对模拟运行测试。上一步需要花费45分钟以上的时间来构建,并且只需要偶尔构建一次,因此我想创建另一个构建触发器,该触发器将仅提取已经构建了我们程序的映像,因此它可以运行测试而不必每次都构建。我遇到的问题是我不确定如何从自定义构建器中保存和导出图像。因为这没有运行gcr.io/cloud-builders/docker构建器,所以我不知道是否有可能在自定义构建器中进行更改并从该自定义构建器中导出新映像(包括所做的更改)而无权访问到标准的docker构建器。可能的解决方案可能只是使用标准docker构建器并使用
run
参数运行容器,并在dockerfile中使用CMD
命令执行构建,然后列出另一个构建步骤以调用docker commit
。但是我想应该有另一种解决方法。感谢您的帮助!
TDLR:我想将docker容器作为Google Cloud Build中的自定义构建器运行,对容器进行更改,然后保存更改并将其作为图像导出到Container Registry,以便无需进行测试即可用于测试程序每次测试前都要花45分钟来构建程序。我怎样才能做到这一点?
steps:
# This step runs builds the docker container which runs flake8, yapf and unit tests
- name: 'gcr.io/cloud-builders/docker'
id: 'BUILD'
args: ['build',
'-t',
'gcr.io/$PROJECT_ID/mysql2datacatalog:$COMMIT_SHA',
'.']
# Create custom image tag and write to file /workspace/_TAG
- name: 'alpine'
id: 'SETUP_TAG'
args: ['sh',
'-c',
"echo `echo $BRANCH_NAME |
sed 's,/,-,g' |
awk '{print tolower($0)}'`_$(date -u +%Y%m%dT%H%M)_$SHORT_SHA > _TAG; echo $(cat _TAG)"]
# Tag image with custom tag
- name: 'gcr.io/cloud-builders/docker'
id: 'TAG_IMAGE'
entrypoint: '/bin/bash'
args: ['-c',
"docker tag gcr.io/$PROJECT_ID/mysql2datacatalog:$COMMIT_SHA gcr.io/$PROJECT_ID/mysql2datacatalog:$(cat _TAG)"]
- name: 'gcr.io/cloud-builders/gsutil'
id: 'PREPARE_SERVICE_ACCOUNT'
args: ['cp',
'gs://my_sa_bucket/mysql2dc-credentials.json',
'.']
- name: 'docker.io/library/python:3.7'
id: 'PREPARE_ENV'
entrypoint: 'bash'
env:
- 'GOOGLE_APPLICATION_CREDENTIALS=/workspace/mysql2dc-credentials.json'
- 'MYSQL2DC_DATACATALOG_PROJECT_ID=${_MYSQL2DC_DATACATALOG_PROJECT_ID}'
args:
- -c
- 'pip install google-cloud-datacatalog &&
system_tests/cleanup.sh'
- name: 'gcr.io/cloud-builders/docker'
id: 'SYSTEM_TESTS'
args: ['run',
'--rm',
'--tty',
'-v',
'/workspace:/data',
'gcr.io/$PROJECT_ID/mysql2datacatalog:$COMMIT_SHA',
'--datacatalog-project-id=${_MYSQL2DC_DATACATALOG_PROJECT_ID}',
'--datacatalog-location-id=${_MYSQL2DC_DATACATALOG_LOCATION_ID}',
'--mysql-host=${_MYSQL2DC_MYSQL_SERVER}',
'--raw-metadata-csv=${_MYSQL2DC_RAW_METADATA_CSV}']
- name: 'gcr.io/cloud-builders/docker'
id: 'TAG_STABLE'
entrypoint: '/bin/bash'
args: ['-c',
"docker tag gcr.io/$PROJECT_ID/mysql2datacatalog:$COMMIT_SHA gcr.io/$PROJECT_ID/mysql2datacatalog:stable"]
images: ['gcr.io/$PROJECT_ID/mysql2datacatalog']
timeout: 15m
构建Docker映像
- 创建标签
- 标签图像
- Pull Service帐户
- 运行对自定义图片进行测试
- 如果成功,则标记自定义图像