## What this fixes
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.
## The fix
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/$PROJECT_ID/my-project:$TAG_NAME', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-project:$TAG_NAME']
- name: 'gcr.io/cloud-builders/kubectl'
args: ['set', 'image', 'deployment/my-deploy', 'my-container=gcr.io/$PROJECT_ID/my-project:$TAG_NAME']
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
## Why it happens
The thread above nails the cause, so the fix addresses that directly rather than symptoms. Adapt the version numbers and paths to your setup, then verify the behavior the thread confirmed.
## Source
https://github.com/GoogleCloudPlatform/cloud-builders/issues/22