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 document 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
There are several Google Cloud orbs available in the CircleCI Orbs Registry that you can use 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
jobs:
# build job ommitted for brevity
deploy-job:
docker:
- image: <docker-image-name-tag>
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
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:
version: 2
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.
Help make this document better
This guide, as well as the rest of our docs, are open source and available on GitHub. We welcome your contributions.
- Suggest an edit to this page (please read the contributing guidefirst).
- To report a problem in the documentation, or to submit feedback and comments, please open an issue on GitHub.
- CircleCI is always seeking ways to improve your experience with our platform. If you would like to share feedback, please join our research community.
Need support?
Our support engineers are available to help with service issues, billing, or account related questions, and can help troubleshoot build configurations. Contact our support engineers by opening a ticket.
You can also visit our support site to find support articles, community forums, and training resources.
CircleCI Documentation by CircleCI is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.