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
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.
- Developer checks in the source code to a Version Control system such as GitHub
- GitHub triggers a post-commit hook to Cloud Build.
- Cloud Build builds the container image and pushes to Container Registry.
- Cloud Build then notifies Cloud Run to redeploy
- 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.
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:
- Builds a tomcat 8.5 instance from scratch
- Clones the source code of a sample web application (You can substitute that with your git repo)
- Builds the application using maven
- Copies the built war file to the
catalina.shas the entry point script
The cloudbuild.yaml file
We will use the following cloud build file:
Contains configuration to use the Google Cloud Build service that
- Builds the docker image in Google Cloud Platform
- Pushes the container image into the Google Cloud Registry
- 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
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.
And you would see that you have created the trigger successfully.
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.
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
Let us investigate and see what is happening within the build
And you would see that Cloud Build is running the steps sequentially.
Wait for the build to complete
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.
Access the Deployed Application
Go to Google Cloud Run, and you will see that the Cloud Build has deployed a new service.
Click on the tomcat service, fetch the URL and access it from the browser and you should see the sample website below
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.