DEV Community

chauhoangminhnguyen
chauhoangminhnguyen

Posted on • Edited on • Originally published at howtodevez.blogspot.com

Github CI/CD with Google Cloud Build

Introduction

  • Continuous Integration (CI): This is the process of building, testing, and performing necessary actions to ensure code quality before it gets merged into the main branch for deployment.
  • Continuous Delivery (CD): This usually happens after CI and includes steps to deploy the source code to various environments like staging and production.

This guide will show you how to set up CI/CD on Github using Google Cloud Build. While Github provides shared runners, if you or your organization have many jobs that need executing during development, setting up your own runner is a better choice.

Before proceeding, you should understand some basics about Google Cloud Run to build and deploy Docker images. You can refer to this article for more details: Build Docker image for NodeJS Typescript Server.

Google cloud build Github

Setting Up GitHub CI/CD

First, create a Github repository. You can choose either a public or private repository.

You can use a NodeJS TypeScript application, following my guide on building a Docker image, or use your existing source code (make sure to include a Dockerfile to build the Docker image).

Next, in your source code, create a cloudbuild.yaml file as follows:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '.', '-t', 'gcr.io/project-id/express-ts:$SHORT_SHA', '-t', 'gcr.io/project-id/express-ts:latest']
- name: 'gcr.io/cloud-builders/docker'
  args: ['image', 'push', '--all-tags', 'gcr.io/project-id/express-ts']
- name: 'gcr.io/cloud-builders/gcloud'
  args:
    - run
    - deploy
    - --region=asia-southeast1
    - --image=gcr.io/project-id/express-ts:latest
    - --max-instances=1
    - --platform=managed
    - --port=3000
    - --allow-unauthenticated
    - express-ts
Enter fullscreen mode Exit fullscreen mode

This file outlines the steps that Google Cloud Build will take when you push code to a specified branch on Github:

- 'gcr.io/cloud-builders/docker' and 'gcr.io/cloud-builders/gcloud' are used to execute Docker and Google Cloud commands.

- $SHORT_SHA represents the short commit ID.

- The first step is to build a Docker image with two tags: the short commit ID and 'latest'.

- Step 2 involves pushing the Docker image with both tags to Google Artifact Registry.

- Step 3 is deploying via Google Cloud Run, as instructed in the previous article.

Note: Remember to replace the project ID and other parameters according to your project.

Create a Trigger

Next, navigate to Google Cloud Build > Create trigger. Then, input the required information, paying attention to the following fields:

Create trigger 1

Create trigger 2

- Repository: Connect to your repository, supporting both Github and Bitbucket. After successful account connection, you can configure for both public and private repositories.

- Branch: Specify the branch name that will trigger Cloud Build upon action. Branch names support regex.

- Cloud Build Configuration File Location: Specify the file name used to define the steps Cloud Build will execute. You can use the default or customize it as needed.

- Service Account: Choose the account corresponding to the project ID used to push Docker images to Google Artifact Registry.

Note that currently, there are usage limits for Google Cloud Build based on region. Check here for more information. You can request an increase in quota or switch to a suitable region to use Cloud Build effectively.

Test CI/CD

Push your source code to Github to test the CI/CD process.

A successful result will be as follows:

Job running

Job done

You can also access Google Cloud Build to check the results.

Google cloud build result

Results after deployment:

Deployed

See you again in the next articles!


If you found this content helpful, please visit the original article on my blog to support the author and explore more interesting content.

BlogspotBlogspotDev.toFacebookX


Some series you might find interesting:

Top comments (0)