GitOps style CI-CD on Google Kubernetes Engine using Cloud Build

Anil Saravade
Sep 16 · 3 min read
Credits: Photo by JJ Ying on Unsplash

Cloud Build is a service that executes your builds on Google Cloud Platform’s infrastructure.

Cloud Build can import source code from a variety of repositories or cloud storage spaces, execute a build to your specifications, and produce artifacts such as Docker containers or Java archives.

You can write a build config to provide instructions to Cloud Build on what tasks to perform. You can configure builds to fetch dependencies, run unit tests, static analyses, and integration tests, and create artifacts with build tools such as docker, gradle, maven, bazel, and gulp.

Credits: cloud.google.com

Prerequisites:

  1. Create a Google Kubernetes Engine cluster.
  2. Create a GitHub repository for version control.
  3. To deploy the application in your Kubernetes cluster, Cloud Build needs the Container Developer IAM Role:
Kubernetes Engine Admin -Source Repository Administrator -Storage Admin

Create a continuous integration pipeline

1. Create the following files in the GitHub repository.

  • Create a sample HTML page.
  • Create a Dockerfile.

We need to create a Dockerfile using the base image as nginx and copy the index.html file inside the image which we’ve created above.

  • Create a continuous delivery pipeline.

The cloudbuild.yaml file describes the deployment process to be run in Cloud Build. It has multiple steps:

a. Clone the Repository from Github.

b. Build the docker image.

c. Push the docker image to Google Container Repository.

d. Generates a new manifest.

e. Cloud Build applies the manifest on the GKE cluster.

Credits: cloud.google.com

Replace the following variables in below snippet.

<github_repository> # Provide your GitHub repository link.<preffered_image_name> # Provide preffered image name.<gke_cluster_zone/region> # Provide the region/zone of your GKE cluster.<gke_cluster_name> # Provide the name of your GKE cluster.
  • Create a Kubernetes Deployment.

Replace the following variables in below snippet.

<preffered_image_name> # Provide preffered image name same as we mentioned in the cloudbuild.yaml.

Push all the files in the GitHub repository.

2. Setup the Cloudbuild trigger.

  1. Open the Triggers page of Cloud Build.
  2. Click Add trigger.
  3. Select “ GitHub” as a source and click Continue.
  4. Select the repository name in which you’ve configured the environment and click Continue.
  5. In the “Triggers settings” screen, enter the following parameters:
  • Name: <provide-cloudbuild-name>
  • Branch (regex): <provide-github-branch-name>
  • Build configuration: cloudbuild.yaml

6. Click Create trigger.

3. Access your application.

  1. SSH into your GKE cluster and execute the below command.
kubectl get services nginx-service

The output is similar to this:

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)    AGE
nginx-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54s

Use the external-ip to access your application.

The CI-CD pipeline is ready. Now everytime you modify the index.html page in GitHub repository the new update content will be updated to your GKE deployment.

Links:


Questions?

If you have any questions, I’ll be happy to read them in the comments. Follow me on medium or LinkedIn.

Searce Engineering

We identify better ways of doing things!

Anil Saravade

Written by

Cloud Engineer @Searce | AWS & GCP Certified | DevOps solutions | Kubernetes & Docker | ML | Former AWS Employee

Searce Engineering

We identify better ways of doing things!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade