Deploying to GKE Stay organized with collections Save and categorize content based on your preferences.
This page explains how to deploy an application to Kubernetes usingCloud Build.
Cloud Build provides agke-deploy builder that enables you to deploy a containerized application to aGKE cluster.
gke-deploy is a wrapper aroundkubectl, the command-line interface forKubernetes. It applies Google's recommended practices for deploying applicationsto Kubernetes by:
Updating the application's Kubernetes resource file to use the containerimage's digest instead of a tag.
Addingrecommended labels to the Kubernetes resource file.
Retrieving credentials for the GKE clustersto which you're deploying the image.
Waiting for the Kubernetes resource file that was submitted to be ready.
If you want to deploy your applications usingkubectl directly and do not needadditional functionality, Cloud Build also provides akubectl builder that you can use to deploy your application to aGKE cluster.
Before you begin
Create aGKE cluster, if you do not have one yet.
Have your containerized application in the form of source code and a
Dockerfileready. Your source code needs to be stored in a repository, such asCloud Source Repositories, GitHub, or Bitbucket.You'll need at least one Kubernetes resource file describing the Kubernetesresources used to run your application. If you do not have Kubernetesresource files, use the following steps to generate one for yourapplication:
- Open theKubernetes Engine clusters page in the Google Cloud console.
- On the Kubernetes Engine clusters page, clickDeploy.
- Select your container and clickContinue. You will see the Configurationsection.
- Under Configuration YAML, clickView YAML to get a sample Kubernetesresource file.
Caution: Effective June 17, 2024, Cloud Source Repositories isn't available to new customers. If your organization hasn't previously used Cloud Source Repositories, you can't enable the API or use Cloud Source Repositories. New projects not connected to an organization can't enable the Cloud Source Repositories API. Organizations that have used Cloud Source Repositories prior to June 17, 2024 are not affected by this change.
Required IAM permissions
Add the Google Kubernetes Engine Developer role to your account:
In the Google Cloud console, go to thesettings Cloud BuildPermissions page:
Set the status of theKubernetes Engine Developer role toEnabled.
Deploying a prebuilt container image
To deploy a particular version of your application withgke-deploy:
Make sure your Kubernetes resource file is referring to thecorrect container image tag or digest.
Add the
gke-deploystep in yourbuild configuration file:YAML
steps:...# deploy container image to GKE-name:"gcr.io/cloud-builders/gke-deploy"args:-run---filename=kubernetes-resource-file---location=location---cluster=clusterJSON
{"steps":[{"name":"gcr.io/cloud-builders/gke-deploy","args":["run","--filename=kubernetes-resource-file","--location=location","--cluster=cluster"]}]}Where:
- kubernetes-resource-file is the file path of your Kubernetes resourcefile or the directory path containing your Kubernetes resource files.
- cluster is the name of the GKE cluster that the applicationwill be deployed to.
- location is the region/zone of the cluster.
For more information on available flags, see
Note: When deploying to multiple GKE clusters,Cloud Build persists the home directory across build steps,which means that thegke-deploy runflags.kubectlcontext of the previous step to be used bysubsequent steps by default. To avoid this, add an environment variabledeclaration,KUBECONFIG=/tmp/kubeconfig, to each step of your buildconfig file.Start your build:
gcloudbuildssubmit--region=REGION--project=project-id--configbuild-configWhere:
- project-id is the ID for your project.
- build-config is the name of your build configuration file.
REGIONis one of thesupported build regions.
Building and deploying a new container image
To build a new container image and deploy the new container image:
Update your Kubernetes resource file with the new containerimage using the
--imageattribute:YAML
steps:# build the container image-name:"gcr.io/cloud-builders/docker"args:["build","-t","gcr.io/project-id/image:tag","."]# push container image-name:"gcr.io/cloud-builders/docker"args:["push","gcr.io/project-id/image:tag"]# deploy container image to GKE-name:"gcr.io/cloud-builders/gke-deploy"args:-run---filename=kubernetes-resource-file---image=gcr.io/project-id/image:tag---location=location---cluster=clusterJSON
{"steps":[{"name":"gcr.io/cloud-builders/docker","args":["build","-t","gcr.io/project-id/image:tag","."]},{"name":"gcr.io/cloud-builders/docker","args":["push","gcr.io/project-id/image:tag"]},{"name":"gcr.io/cloud-builders/gke-deploy","args":["run","--filename=kubernetes-resource-file","--image=gcr.io/project-id/image:tag","--location=location","--cluster=cluster"]}]}Where:
- project-id is the ID for your project.
- image is the desired name of the container image, usually the applicationname.
- tag is the tag of the container image.
- If you are building a new container image with each commit, a good practiceis to use the commit short SHA as a tag. Cloud Build makes thisavailable as adefault substitution,
$SHORT_SHA.
- If you are building a new container image with each commit, a good practiceis to use the commit short SHA as a tag. Cloud Build makes thisavailable as adefault substitution,
- kubernetes-resource-file is the file path of your Kubernetes resourcefile or the directory path containing your Kubernetes resource files.
- cluster is the name of the GKE cluster that the applicationwill be deployed to.
- location is the region/zone of the cluster will be deployed to.
gke-deploycan only reference one image in your build configurationfile. If your Kubernetes resource files contain multiple imagereferences, only the images beginning withgcr/project-id/imagewillbe updated to reference the specified tag.Start your build:
gcloudbuildssubmit--region=REGION--project=project-id--configbuild-configWhere:
- project-id is the ID for your project.
- build-config is the name of your build configuration file.
REGIONis one of thesupported build regions.
Automating deployments
You can automate the deployment of your application to GKE bycreating a trigger in Cloud Build. You can configure triggers tobuild and deploy images whenever you push changes to your code.
To create a build trigger:
Open theTriggers page in the Google Cloud console:
Select your project from the project selector drop-down menu at the top of the page.
ClickOpen.
ClickCreate trigger.
On theCreate trigger page, enter the following settings:
Enter a name for your trigger.
Select the repository event to start your trigger.
Select the repository that contains your source code and build config file.
Specify the regex for the branch or tag name that will start your trigger.ee
Choose aConfiguration for your trigger.
If you choose a Cloud Build configuration file, you can specifySubstitution variables by providing a variable name and the value you want to associate with that variable. In the example below, the user-defined substitution variable
_CLOUDSDK_CONTAINER_CLUSTERspecifies the cluster to deploy to, and the user-defined substitution variable_CLOUDSDK_COMPUTE_ZONEspecifies its location. If you want to deploy to a different cluster, you can use the same build configuration and only need to change the values of the substitution variables:YAML
steps:...# deploy container image to GKE-name:"gcr.io/cloud-builders/gke-deploy"args:-run---filename=kubernetes-resource-file---image=gcr.io/project-id/image:tag---location=${_CLOUDSDK_COMPUTE_ZONE}---cluster=${_CLOUDSDK_CONTAINER_CLUSTER}JSON
{"steps":[{"name":"gcr.io/cloud-builders/gke-deploy","args":["run","--filename=kubernetes-resource-file","--image=gcr.io/project-id/image:tag","--location=${_CLOUDSDK_COMPUTE_ZONE}","--cluster=${_CLOUDSDK_CONTAINER_CLUSTER}"]}]}Where:
- kubernetes-resource-file is the file path ofyour Kubernetes configuration file or the directory path containingyour Kubernetes resource files.
- project-id is the ID for your project.
- image is the desired name of the container image, usually the application name.
- tag is the tag of the container image.
To learn more about how to define substitutions for build configuration files, seeUsing user-defined substitutions.
ClickCreate to save your build trigger.
When you push code to your repository, Cloud Build will automaticallytrigger a build. To learn more about build triggers, seeCreating and managingbuild triggers.
What's next
- Learn how toconfigure automated deployments for your workloads on GKE.
- Learn how tocreate a GitOps-style CI/CD pipeline deploying toGKE with Cloud Build.
- Learn about additional
gke-deployoptions. - Learn how toperform blue/green deployments on Compute Engine.
- Learn about additionalCloud builders.
- Learn how totroubleshoot build errors.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.