If you’re new to Google Cloud Run, feel free to catch up by reading Deploying Containerized Application with Google Cloud Run.

Developers love to focus on writing codes while automating deployment processes. Google Cloud Platform provides such flexibility by providing tools to make deployment seem effortless.

In this article, our focus would be to integrate Cloud Build Trigger with Cloud Source Repositories, GitHub or Bitbucket and also setup continuous deployment to Google Cloud Run.

Our source codes can be found on GitHub here:

Timtech4u/node-cloud-run-cd
Nodejs Continuous deployment with Google Cloud Run - Timtech4u/node-cloud-run-cdgithub.com

Continuous Deployment with Cloud Build

Cloud Build Triggers automatically builds our image whenever there are changes pushed to the build source. We could build our images either by using a Dockerfile or cloudbuild.yaml ( our build config can also be a JSON file).
In addition, Cloud Build offers 120 free build-minutes per day.

We’ll be using a  cloudbuild.yaml to perform the following operations:

  • Build the Docker Image
  • Push the image to the Container Registry
  • Deploy a new revision to the Cloud Run service
steps:
  # build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/projectz-239507/app', '.']
  # push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/projectz-239507/app']
  # Deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['beta', 'run', 'deploy', 'app', '--image', 'gcr.io/projectz-239507/app', '--region', 'us-central1']
images:
- gcr.io/projectz-239507/app
timeout: 1200s

Replace Project ID [projectz-239507] and Service Name [app] with your own values.

Elevate Service Account Permissions

We need to add the Cloud Run Admin and Service Account User roles to our Cloud Build Service Account [@cloudbuild.gserviceaccount.com].

Visit IAM and Admin Page and modify the Service Account as follows:

@cloudbuild.gserviceaccount.com roles elevated.

Setup Cloud Build Triggers

We’ll proceed to creating a trigger that listens to changes on a particular branch on our source codes and performs the operation in our cloudbuild.yaml file.

Visit the Cloud Build Triggers Page

Build Triggers Page

Select your Source Code option and Continue.

Select Source

Define your trigger, enter it’s Name, Trigger Type and Branch, Build Configuration ( as mentioned, we’ll be making use of the cloudbuild file).

Create Trigger

Click on Create Trigger. You can monitor the Trigger on the History tab.

😀 Service [app] revision [app-00002] has been deployed and is serving traffic at https://app-nchqhrxakq-uc.a.run.app

From this point on, anytime you push to your repository, you automatically trigger a build and a deployment to your Cloud Run service.
You can learn more about Cloud Build from here. 🕺🕺🕺

Thanks for reading through! Let me know if I missed any step, if something didn’t work out quite right for you or if this guide was helpful.