Automating GCP Instance Groups using Jenkins & Cloudbuild

Mohak
Searce
Published in
4 min readJan 14, 2022

--

ptrust.org

In today’s technological era where we do multiple changes on day after day within our virtual machine one conjointly desires machine-controlled or automated method to deploy it quickly to production , this article demonstrate about “How to update any changes done within a virtual machine to MIG via Jenkins pipeline that triggers Cloud Build”

Key components used:

  • A Virtual Machine (VM) may be a reason resource that uses computer code rather than a physical laptop to run programs and deploy apps. every virtual machine runs its own software system and functions one by one from the opposite VMs. (For more details, please refer to this link)
  • Managed Instance Group (MIG) is a GCP Compute Engine Resource that’s a set of VM instances managed as one entity. Autoscaling helps your applications effortlessly handle will increase in traffic, whereas reducing value once the necessity for resources is lower. (For more details, please refer to this link)
  • Jenkins is an open source automation tool. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery. (For more details, please refer to this link)
  • Cloud Build is a service that executes your builds on Google Cloud Platform’s infrastructure. (For more details, please refer to this link)
  • An Instance Template is a resource that you can use to create virtual machine (VM) instances and managed instance groups (MIGs). (For more details, please refer to this link)

Now first let’s understand how flow works!

Here I actually have created one cloudbuild.yaml file, which will create one golden image using virtual machine’s disk as a supply disk, then it’ll produce one instance example, which is able to be updated within your various MIG before checking the steadiness of your MIG.

Pre-requisites:

  1. Jenkins server version 2.324, see guide to install https://www.jenkins.io/doc/book/installing/
  2. Two service accounts:
  • CloudBuild Service Account with roles attached:
    - Compute Instance Admin (v1)
    - Service Account User
    - Cloud Build Service Account
    - Cloud API Gateway Service Agent
  • Jenkins Service Account with roles attached:
    - Service Account User
    - Cloud Build Service Account
    - Compute Instance Admin (v1)

3. Enable APIs : —

  • Cloud Build(cloudbuild.googleapis.com)
  • Compute Engine(compute.googleapis.com)

4. Jenkins plugin installed :

  • Git (For more details, please refer to this link)
  • Google Cloud Build (For more details, please refer to this link)

5. One virtual machine in which we do the changes and create golden image (For more details, please refer to this link)

6. One Instance template (Specification of VM)

7. One MIG (Managed Instance Group)

Deployments steps:

Step 1: Creating and downloading service account keys

  • We would require service account key to authenticate our gcp project.
  • Download your Jenkins Service Account Keys from console(refer this link) and add them in your Jenkins Global Credentials (Refer Screenshot below), use your GCP project name as Project Name.

Step 2: Creating Jenkins project & pipeline

  • Now, create one Jenkins Project(refer this link) and below code as pipeline:
  • In below pipeline, we will:
    - Create one golden image where changes are done
    - Create one instance template using golden image created before
    - It will wait for MIG to become stable
    - At last it will apply the new instance template on MIGs. Now, the final changes will get applied inside the MIG.
https://gist.github.com/mohak-aws/76ad3c73a048a643a67f19e61b1183c5

When you click on “Build Now”, it will look something like below:

Hence, no matter changes you probably did on the running supply instance can currently mirror in instance within the MIG!

Let’s conclude with a summary!

We have achieved quick and simple automated process for updating images with autoscaling feature inside an MIG using cloud build which is triggered through a Jenkins pipeline.

So, firstly the cloud build has created a image from the source disk of the vm, secondly the newly created image is now attached to a new template, then the template is now updated inside MIG. The limitation of this blog is that Rollback cannot be performed.

Thanks for reading, keep learning keep sharing!

--

--