Google Cloud DevOps Series: Continuous Integration / Continuous Deployment Workflow (CICD)

Google Cloud DevOps Series: Part-4

Anchit Nishant
Google Cloud - Community
4 min readNov 12, 2021

--

Welcome to Part 4 of the Google Cloud DevOps series.. You can find the complete series Here

Continuous Integration and Continuous Deployment : Hands on Demo

We have already cloned the Online Boutique application in the previous Blog. In this demo we will do Continuous Integration and Continuous Deployment into QA, Staging and Production environments.

Enable the Google Cloud Deploy, Cloud Build, GKE, and Cloud Storage APIs

Create a Google Cloud repository and push the application to the Repository.

PROJECT_ID=<your project ID>
SOURCE_REPO_NAME=<name of repository>
gcloud source repos create $SOURCE_REPO_NAME

Push the Online boutique application to the newly created repository.

Create 3 different GKE environments for QA, Staging and Production.

REGION=<region name eg. us-central1>gcloud container clusters create qa-cluster --region=$REGION --scopes=cloud-platform --workload-pool=${PROJECT_ID}.svc.id.googgcloud container clusters create staging-cluster --region=$REGION --scopes=cloud-platform --workload-pool=${PROJECT_ID}.svc.id.googgcloud container clusters create production-cluster --region=$REGION --scopes=cloud-platform --workload-pool=${PROJECT_ID}.svc.id.goog

Create a Google Cloud Artifact Registry

ARTIFACT_REPO_NAME=<name of artifact repository>gcloud artifacts repositories create $ARTIFACT_REPO_NAME --repository-format=docker --location=$REGION

Create a Build trigger

gcloud beta builds triggers create cloud-source-repositories  --repo=$SOURCE_REPO_NAME --branch-pattern=”^master$” --build-config=cloudbuild.yaml

Grant Cloud Build service account permission to operate Cloud Deploy.

  1. Open the IAM page:
  2. Select your Cloud project.
  3. In the permissions table, locate the row with the email address ending with @cloudbuild.gserviceaccount.com. This is your Cloud Build service account.
  4. Click on the pencil icon.
  5. Select the role Cloud Deploy Operator and grant it to the Cloud Build service account.
  6. Click Save.

Add the Google Kubernetes Engine Developer role to your account:

  1. Open the Cloud Build Settings page, You’ll see the Service account permissions page
  2. Set the status of the Kubernetes Engine Developer role to Enabled.

Create clouddeploy.yaml file at the root of the code base, as below:

apiVersion: deploy.cloud.google.com/v1
kind: DeliveryPipeline
metadata:
name: boutique
description: main application pipeline
serialPipeline:
stages:
- targetId: qa
profiles: []
- targetId: staging
profiles: []
- targetId: prod
profiles: []
---
apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
name: qa
description: development cluster
gke:
cluster: projects/<project-id>/locations/<region>/clusters/qa-cluster
---
apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
name: staging
description: production cluster
gke:
cluster: projects/<project-id>/locations/<region>/clusters/staging-cluster
---apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
name: prod
description: production cluster
gke:
cluster: projects/<project-id>/locations/<region>/clusters/production-cluster

Change the Cloudbuild.yaml to include Artifact Registry and trigger Cloud Deploy, as below:

steps:
- id: 'prepare build and run skaffold'
name: 'gcr.io/k8s-skaffold/skaffold:v0.20.0'
entrypoint: 'bash'
args:
- '-c'
- >
gcloud auth configure-docker <region>-docker.pkg.dev;
skaffold build -f=skaffold.yaml \
--build-concurrency=0 \
--default-repo=<artifact registry endpoint> \
--file-output /workspace/artifacts.json;
- id: 'Deploy to cluster'
name: 'google/cloud-sdk:latest'
entrypoint: 'bash'
args:
- '-c'
- >
gcloud deploy apply --file clouddeploy.yaml --region=<region> --project=<project-id>;
gcloud deploy releases create boutique-$SHORT_SHA \
--delivery-pipeline boutique \
--description "$(git log -1 --pretty='%s')" \
--region=<region> \
--build-artifacts /workspace/artifacts.json
# Add more power, and more time, for heavy Skaffold build
timeout: '3600s'
options:
machineType: 'N1_HIGHCPU_8'

Push the code to the repository using git commands.

You can trace the Cloud Deploy console and promote the deployment to next stage.

Now you can make any changes to the application and push the updated code to the repository, the Cloud Build will be triggered automatically and the Cloud Deploy pipeline will be executed.

Coming up…

In this blog, we learned how to implement end to end CI/CD workflow on Google cloud for Samajik’s containerised workloads. In our next conversation we will understand how Logging and Monitoring operations are performed in Google Cloud. Stay tuned to Ram and Guhan’s conversation to know more about this.

Contributors: Pushkar Kothavade, Shijimol A K, Dhandus, Tushar Gupta

Update: You can read Part-5 here.

--

--