Deploy to Cloud Run with CI/CD | GCP

Sakshi Khandelwal
Google Cloud - Community
6 min readApr 29, 2023

Have you ever thought of deploying your application to GCP through CI/CD? That application could be any Java, Python, or Node.js application.

What we’d like is to push our code into a repository and have it deployed on GCP automatically and in the right way. It should give us the surety that the code is tested before deployments go into different stages(like dev, test, pre-prod, and prod).

If yes, let’s move your application to GCP!

Objective:

To learn about:

CI/CD Pipeline
  • Cloud Source Repository
  • Cloud Build
  • Artifact Registry
  • Cloud Run

Pre-requisites:

  • The application which you wanted to deploy
  • A GCP account

Overview:

  • Cloud Source Repository: Cloud Source Repositories are fully featured, private Git repositories hosted on Google Cloud.
  • Cloud Build: 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.
  • Artifact Registry: Artifact Registry is a single place for your organization to manage container images and language packages (such as Maven and npm). It is fully integrated with Google Cloud’s tooling and runtimes and comes with support for native artifact protocols. This makes it simple to integrate it with your CI/CD tooling to set up automated pipelines.
  • Cloud Run: A fully managed compute platform that automatically scales containers. Build apps in your favorite language and deploy them in seconds.

Let’s Start:

The first step of our walk is to log in/signup for GCP Account and create a separate Project for the demo.

Cloud Source Repository:

Go to Cloud Source Repository and click on ‘Create new repository’.

Give the repository name with the new project created as the Project and click on ‘Create’.

As we have already the application ready to deploy, we will select the second option below to push the code from the local system. Select the second tab as ‘Google Cloud SDK’.

Now, open Cloud Console from the top right button. And activate Cloud Shell as below:

Now clone the repository from the command in the earlier screen as shown below:

Now open the editor from the top right button ‘Open Editor’ of Cloud Shell Terminal. It will show the empty repository as below. Just drag and drop all your application code into the repository.

Now, we need to commit the code to Cloud Source Repository. Go back to the Cloud Shell and run the cd command on your new project. Commit all the code like below:

Now, let's go back to Cloud Source Repository. We should see our newly added files:

Till now we have created Cloud Source Repository and committed our code. In the next step, we are going to configure Cloud Build to pick up the source from Cloud Source Repository, build it, and deploy it to Cloud Run.

Cloud Build:

Below is the cloudbuild.yaml file which I have committed to the repository and mentioned the steps as per my simple Python application.

This file indicates the steps to build the docker image, push it and deploy the same. Now, we need to set up the Cloud Build trigger to start the CI/CD pipeline. So, go to Cloud Build. Select Trigger from the left tab and click on ‘Create Trigger’ as shown below:

Give the trigger name. We want the Event as ‘Push to a branch’, so, it will remain as is. Select the Repository from the dropdown and the default branch will get populated as below:

Other options will also remain the same. Now, just click on the ‘Create’ button. So, this would create the trigger and this trigger will be executed when we push in the branch.

Before we execute the build, we need to configure the permission. In our use case, we want our build to get deployed to Cloud Run. Select ‘Settings’ from the left tab and enable Cloud Run as below.

Artifact Registry:

Go to the Artifact Registry and click on ‘Create Repository’. Give the name and select the region as below:

And click on ‘Create’.

Cloud Run:

Now, let’s run the trigger to deploy the code in the Cloud Run. Go back to the Trigger tab in Cloud Build and click on ‘Run’.

After a couple of min or so, all the 3 steps of our Cloud Build should be completed as shown below:

Now, open the Cloud Run Screen and you can see your newly created Cloud Run as shown below:

We have not created or configured anything manually for Cloud Run. Everything is handled by the Cloud Build pipeline. Just click on the Cloud Run name and you should be able to see the URL like below:

Click on the URL and your application is there!

You can play more by committing any change in the code. It will be auto-deployed in the Cloud Run.

Common Error:

* Invalid build: Invalid Image Name: This error can come while triggering the build. It can be seen below:

You just need to shorten your image name in the docker file. Maybe you can try to remove the repo name from the image name. It seems Cloud Build is supporting only 100 characters. Just a tip to reduce your debug time. :)

Hope you like it.

If you have any related queries, please free to contact me. I’ll be happy to help you if I could. Stay Connected :)

If you liked this post, please clap for it. I would really appreciate it.

Twitter: https://twitter.com/SakshiKhandlwl
LinkedIn: www.linkedin.com/in/sakshikhandelwal276

Happy coding!

--

--

Sakshi Khandelwal
Google Cloud - Community

Java 8 Certified | AWS Certified Developer-Associate| SSE at MasterCard | Learner