How to CI/CD on Google Cloud Platform

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

Gaurav Agarwal
Apr 27, 2020 · 5 min read
Image for post
Image for post
Photo by Everson Mayer on Unsplash

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.

Image for post
Image for post
Google Cloud CI/CD
  1. Developer checks in the source code to a Version Control system such as GitHub

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

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

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

Image for post
Image for post
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.

Image for post
Image for post
Create Trigger

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

Image for post
Image for post
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.

Image for post
Image for post
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

Image for post
Image for post
Cloud Build

Let us investigate and see what is happening within the build

Image for post
Image for post
Cloud Build Running

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

Wait for the build to complete

Image for post
Image for post

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.

Image for post
Image for post
Container Registry

Access the Deployed Application

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

Image for post
Image for post
Cloud Run deployed
Image for post
Image for post
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

Image for post
Image for post
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 Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Gaurav Agarwal

Written by

Certified Kubernetes Administrator | Cloud Architect | DevOps Enthusiast | Connect @ https://gauravdevops.com | https://freedevtools.net

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Gaurav Agarwal

Written by

Certified Kubernetes Administrator | Cloud Architect | DevOps Enthusiast | Connect @ https://gauravdevops.com | https://freedevtools.net

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store