从工件注册表中删除图像时如何忽略 cloudbuild.yaml 中的 "image not found" 错误?

How to ignore "image not found" error in cloudbuild.yaml when deleting images from the artifact registry?

目前 cloudbuild.yaml 看起来像这样:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: [ 'artifacts', 'docker', 'images', 'delete', 'location-docker.pkg.dev/$PROJECT_ID/repository/image' ]
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'location-docker.pkg.dev/$PROJECT_ID/repository/image:latest', './' ]
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'location-docker.pkg.dev/$PROJECT_ID/reporitory/image:latest']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args: ['run', 'deploy', 'image', '--image', 'us-east1-docker.pkg.dev/$PROJECT_ID/cb-area52-ar/area52-image:latest', '--region', 'region']


images:
- 'location-docker.pkg.dev/$PROJECT_ID/registry/image:latest'

基本上执行以下操作:

  1. 删除工件注册表中的现有图像
  2. 建立新形象
  3. 将其推回工件注册表
  4. 将其部署到 google 云 运行

我现在的问题是,只要注册表中没有图像,第一步就会失败。

发生这种情况时如何防止它取消整个构建过程?

您可以创建一个内联脚本来检查图像是否存在。这假设您总是想删除带有 latest 标签的图像。

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  args:
  - '-eEuo'
  - 'pipefail'
  - '-c'
  - |-
    if [[ -z `gcloud artifacts docker images describe location-docker.pkg.dev/$PROJECT_ID/repository/image:latest --verbosity=none --format=text` ]]
    then
      echo "Image does not exist. Continue with the build"
    else
      echo "Deleting Image"
      gcloud artifacts docker images delete location-docker.pkg.dev/$PROJECT_ID/repository/image
    fi