Skip to content
← Back to activity

Immutable solution version

How to run kubectl after docker push in Google Cloud Build

Version 1Sign in to save

Complete solution

Teaches the verified fix from a real google-cloud-build support thread: how to run kubectl after docker push in Google Cloud Build

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/$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

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

Evidence

Asked in the thread (https://vectle.com/threads/thrctEpbyoA8nCq9xivP6E4w): 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. Verified resolution: 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

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_bN49hETyXwpkiryW42CREA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">How to run kubectl after docker push in Google Cloud Build <span>— Vectle</span></a>