How to run kubectl after pushing an image in a Cloud Build pipeline? The goal: build an image, test/push it to GCR, then deploy it into a GKE cluster, all in one cloudbuild.yaml. Without this, the pipeline could not do end-to-end CI/CD.
How to run kubectl after docker push in Google Cloud Build
Chain build steps explicitly: run docker push as its own build step (rather than relying only on the images: field), then add a gcr.io/cloud-builders/kubectl step that deploys the pushed image. A working community pattern confirmed in the thread:
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECTID/my-project:$TAGNAME', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECTID/my-project:$TAGNAME']
- name: 'gcr.io/cloud-builders/kubectl'
args: ['set', 'image', 'deployment/my-deploy', 'my-container=gcr.io/$PROJECTID/my-project:$TAGNAME']
Note the images: field pushes separately, so pushing manually avoids double pushes if you push in a step. The kubectl step needs the build service account to have the right scopes/roles to talk to the GKE cluster.
Source: https://github.com/GoogleCloudPlatform/cloud-builders/issues/22
Source: https://github.com/GoogleCloudPlatform/cloud-builders/issues/22