How to CI/CD on Google Cloud Platform

Article:How-to-CICD-on-Google-Cloud-Platform_Photo by Everson Mayer on Unsplash

By Gaurav Agarwal.Apr 27, 2020

Using Cloud Build, Google Container Registry, and Cloud Run to continuously build and deploy a simple Java application

Google Cloud Platform is one of the leading cloud providers in the public cloud market. It provides a host of managed services, and if you are running exclusively on Google Cloud, it makes sense to use the managed CI/CD tools that Google Cloud provides.

A typical Continuous Integration & Deployment setup on Google Cloud Platform looks like the below.

How to CICD on Google Cloud Platform 2
Google Cloud CI/CD
  1. Developer checks in the source code to a Version Control system such as GitHub
  2. GitHub triggers a post-commit hook to Cloud Build.
  3. Cloud Build builds the container image and pushes to Container Registry.
  4. Cloud Build then notifies Cloud Run to redeploy
  5. Cloud Run pulls the latest image from the Container Registry and runs it.

In this mini-guide, we will use Google Cloud Build to build a simple java application, store the docker image in Google Container Registry, and deploy it to Google Cloud Run.


Artefacts

We need to create the following artefacts for the process to work:

You can fork the https://github.com/bharatmicrosystems/tomcat.git repository as an example. We will use this sample repository in this guide.

The Docker file

We will use the following Docker file for this guide:

Dockerfile

The Dockerfile:

  1. Builds a tomcat 8.5 instance from scratch
  2. Clones the source code of a sample web application (You can substitute that with your git repo)
  3. Builds the application using maven
  4. Copies the built war file to the webapps directory as ROOT.war
  5. Runs catalina.sh as the entry point script

The cloudbuild.yaml file

We will use the following cloud build file:

cloudbuild.yaml

Contains configuration to use the Google Cloud Build service that

  1. Builds the docker image in Google Cloud Platform
  2. Pushes the container image into the Google Cloud Registry
  3. Deploys the image to Google Cloud Run

So let’s get started.


Create the Trigger

The next step is to create a trigger. Ensure that the Cloud Build, Cloud Run and Google Container Registry APIs are enabled

Go to Cloud Build and click on Connect Repository to provide Cloud Build access to your repository. Select your appropriate VCS Repository (GitHub in this case) in the next page and click on Next

That will take you to the GitHub page where you need to authenticate, and an OAuth flow will link your GitHub account with Cloud Run.

Select the repository you want Cloud Run to connect to

How to CICD on Google Cloud Platform 3
Select Repository

And then click on Connect Repository.

In the next page, select create a trigger and provide the name, select the Repository and select all Branches as the branch. Keep the default values for the rest of the configuration and click on next.

How to CICD on Google Cloud Platform 4
Create Trigger

And you would see that you have created the trigger successfully.

How to CICD on Google Cloud Platform 5
Trigger Created

We also need to ensure if Cloud Build has access to deploy to Cloud Run. For that go to settings and Enable the service account permission for Cloud Run.

How to CICD on Google Cloud Platform 6
Cloud Run Enabled

And that’s it. We are ready to test!


Testing the configuration

Make a minor change in the README.md file and push to the remote repository see if it triggers the cloud build

If you have set up everything correctly, if you go to History, you would see that the build has started successfully

How to CICD on Google Cloud Platform 7
Cloud Build

Let us investigate and see what is happening within the build

How to CICD on Google Cloud Platform 8
Cloud Build Running

And you would see that Cloud Build is running the steps sequentially.

Wait for the build to complete

How to CICD on Google Cloud Platform 9

And you see that the build is successful, we are ready to access our application.

Access the Built Image

Once the build is complete, go to Google Container Registry and check if Cloud Build has pushed an image to it.

How to CICD on Google Cloud Platform 10
Container Registry

Access the Deployed Application

Go to Google Cloud Run, and you will see that the Cloud Build has deployed a new service.

How to CICD on Google Cloud Platform 11
Cloud Run deployed
How to CICD on Google Cloud Platform 12
Cloud Run URL

Click on the tomcat service, fetch the URL and access it from the browser and you should see the sample website below

How to CICD on Google Cloud Platform 13
Sample Website

Conclusion

Thanks for reading through! I hope you enjoyed the article. That was a very simplistic example of how to use Google Cloud Build, Container Registry, and Google Cloud Run to create a CI/CD pipeline, and you can customise it according to your requirements.


The original article published on Medium.

Related Posts

Category