如何在 GCP 中使用 cloudbuild.yaml 创建 CI/CD 管道?

How can I create CI/CD pipeline with cloudbuild.yaml in GCP?

我正在尝试创建一个简单的 CI/CD 管道。客户端制作git push后,会启动一个触发器,下面是cloudbuilder.yaml:

# steps:
# - name: 'docker/compose:1.28.2'
#   args: ['up', '-d']
# - name: "docker/compose:1.28.2"
#   args: ["build"]
# images: ['gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose']
# - name: 'gcr.io/cloud-builders/docker'
#   id: 'backend'
#   args: ['build','-t', 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest','.']
# - name: 'gcr.io/cloud-builders/docker'
#   args: ['push', 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest']

# steps:
# - name: 'docker/compose:1.28.2'
#   args: ['up', '-d']
# - name: "docker/compose:1.28.2"
#   args: ["build"]
# images:
# - 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose'

# In this directory, run the following command to build this builder.
# $ gcloud builds submit . --config=cloudbuild.yaml
substitutions:
  _DOCKER_COMPOSE_VERSION: 1.28.2
steps:
- name: 'docker/compose:1.28.2'
  args:
  - 'build'
  - '--build-arg'
  - 'DOCKER_COMPOSE_VERSION=${_DOCKER_COMPOSE_VERSION}'
  - '-t'
  - 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest'
  - '-t'
  - 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:${_DOCKER_COMPOSE_VERSION}'
  - '.'
- name: 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose'
  args: ['version']

images:
- 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest'
- 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:${_DOCKER_COMPOSE_VERSION}'
tags: ['cloud-builders-community']

它 returns 下面的错误:它无法在存储库中创建图像。我该如何解决这个问题?

ERROR: failed to pull because we ran out of retries.
ERROR
ERROR: build step 1 "gcr.io/internal-invoicing-solution/cloudbuild-demo-dockercompose" failed: error pulling build step 1 "gcr.io/internal-invoicing-solution/cloudbuild-demo-dockercompose": generic::unknown: retry budget exhausted (10 attempts): step exited with non-zero status: 1
```


在第二步拉取你的镜像之前,你需要推送它。当您在 yaml 定义的末尾声明 images 时,图像会自动推送到管道的末尾。这里你需要它在中间。


编辑 1

我刚刚添加了一个 docker 推送步骤,从您的评论中复制并粘贴。有用吗?

substitutions:
  _DOCKER_COMPOSE_VERSION: 1.28.2
steps:
- name: 'docker/compose:1.28.2'
  args:
  - 'build'
  - '--build-arg'
  - 'DOCKER_COMPOSE_VERSION=${_DOCKER_COMPOSE_VERSION}'
  - '-t'
  - 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest'
  - '-t'
  - 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:${_DOCKER_COMPOSE_VERSION}'
  - '.'
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest']
- name: 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose'
  args: ['version']

images:
- 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:latest'
- 'gcr.io/$PROJECT_ID/cloudbuild-demo-dockercompose:${_DOCKER_COMPOSE_VERSION}'
tags: ['cloud-builders-community']