Deploy to Google Cloud Platform
In this how-to guide, you will learn how to configure CircleCI to deploy to Google Cloud Platform.
Introduction
Before deploying to Google Cloud Platform, you will need to authorize the Google Cloud SDK and set default configuration settings. Refer to the Authorizing the Google Cloud SDK page for full details.
In addition to the orbs described below, CircleCI has created an GCP convenience image focusing on, deployment: cimg/gcp
.
Using Google Cloud orbs
Several Google Cloud orbs are available in the CircleCI Orbs Registry. Use the orbs to simplify your deployments. For example, the Google Kubernetes Engine (GKE) orb has a pre-built job to build and publish a Docker image, and roll the image out to a GKE cluster, as follows:
Make sure to replace any placeholder versions in the example.
version: 2.1
orbs:
gke: circleci/gcp-gke@x.y.z # Use the GCP GKE orb in your config
workflows:
main:
jobs:
- gke/publish-and-rollout-image:
cluster: <your-GKE-cluster> # name of GKE cluster to be created
container: <your-K8-container-name> # name of your Kubernetes container
deployment: <your-K8-deployment-name> # name of your Kubernetes deployment
image: <your-image> # name of your Docker image
tag: $CIRCLE_SHA1 # Docker image tag - optional
Deployment to GKE with 2.0 configuration
In the following example, if the build-job
passes and the current branch is main
, CircleCI runs the deployment job.
version: 2.1
jobs:
# build job ommitted for brevity
deploy-job:
docker:
- image: <docker-image-name-tag>
working_directory: /tmp/my-project
steps:
- run:
name: Deploy Main to GKE
command: |
# Push Docker image to registry, update K8s deployment to use new image - `gcloud` command handles authentication and push all at once
sudo /opt/google-cloud-sdk/bin/gcloud docker push us.gcr.io/${PROJECT_NAME}/hello
# The new image is now available in GCR for the GCP infrastructure to access, next, change permissions:
sudo chown -R ubuntu:ubuntu /home/ubuntu/.kube
# Use `kubectl` to find the line that specifies the image to use for our container, replace with image tag of the new image.
# The K8s deployment intelligently upgrades the cluster by shutting down old containers and starting up-to-date ones.
kubectl patch deployment docker-hello-google -p '{"spec":{"template":{"spec":{"containers":[{"name":"docker-hello-google","image":"us.gcr.io/circle-ctl-test/hello:'"$CIRCLE_SHA1"'"}]}}}}'
workflows:
build-deploy:
jobs:
- build-job
- deploy-job:
requires:
- build-job # Only deploy once the build job has completed
filters:
branches:
only: main # Only deploy on the main branch
For another example, see our CircleCI Google Cloud deployment example project.