Post

Automate your project with Google Cloud Build

Automate deployment is a useful scheme associated with Git concepts.

Automate your project with Google Cloud Build

Automate deployment is a useful scheme associated with Git concepts. We can write code for many projects and push to git for version controls. Then we should gain benefits from that for deliver those product to our platforms.

Here is the link I wrote about how to start with git.


What is CI/CD

CI/CD stands for “Continuous Integration / Continuous Delivery”. This is a significant setup for automate deployment.

cicd

Continuous Integration covers the way we can integrate the source code from git and automate test. Continuous Delivery goes further with delivering those fully tested code to deploying on the servers.

The work is running by the steps defined in the build definition e.g. unit test, then integration test, finally deploy.


What is Google Cloud Build

Google Cloud Build is one of the CI/CD tool, other you may hear the name are Jenkins and Github Action, for instance.

When we enabled the Google Cloud Build API, we now ready for that.

cloud build api

This is the build history. All builds can be viewed here.

build history

And this is the trigger page. All triggers can be seen and we can manage like create, delete, update, and disable on this page.

trigger

We can connect to other services like send notifications on Google Chat when the build is complete. See the link below.

This time we are going to deploy CI/CD pipeline on Google Cloud Build. Let’s go!


Quest today

Let’s give a story, we want to upload validated JSON files from our repo to Google Cloud Storage. The steps should be:

  1. Verify all JSON files are correct
  2. Copy all JSON files to Google Cloud Storage.

Interested? Let’s go!


Connect to the repo

There are several connectors from many git providers to Google Cloud Build. I use Github so I wanna show 2 simplest ways here.

1. Github App

We can just install the Cloud Build Github app till finish and we can create a trigger on the connection. The app is here but we can install through these steps.

1.1. Connect Repository

connect repo

1.2. Authenticate and install app

install app

1.3. Install on Github page

install github page

1.4. Select repo

select repo

2. Mirror the repo

This solution is also easy. We can create a repo in Google Cloud Source Repository by mirroring the real repo in Github or BitBucket. Follow these steps.

2.1. create a repo in Cloud Source Repo

First thing first, create a repo on Google Cloud Source Repository. Go to https://source.cloud.google.com/ and create a repo.

gsr repo

2.2. Connect external repository

connect external repo

2.3. Authorize to Github

Authorize

2.4. Select a repo

select repo

2.5. Mirror completed

complete


Create a trigger

Next we can create a trigger to do the CI/CD job.

We can manually create a trigger on the web at https://console.cloud.google.com/cloud-build/triggers. Alternatively using gcloud command like this.

1. Connect repo through Github app

1
2
3
4
5
6
7
gcloud beta builds triggers create github \
--name=<TRIGGER_NAME> \
--region=<REGION> \
--repo-name=<REPO_NAME> \
--repo-owner=<OWNER_NAME> \
--branch-pattern=<BRANCH_PATTERN> \
--build-config=<CLOUDBUILD_YAML_FILEPATH>
  • TRIGGER_NAME: name of this trigger. Usually express project name, environment, and branch.
  • REGION: region of the trigger
  • REPO_NAME: name of the source repo.
  • OWNER_NAME: owner of the repo
  • BRANCH_PATTERN: branch pattern e.g. ^main$ means branch “main” or ^feature-.* means branches begins with “feature-“
  • CLOUDBUILD_YAML_FILEPATH: the path of build definition. Usually use “cloudbuild.yaml” for Google Cloud Build here.

connect github

2. Connect repo through mirroring

1
2
3
4
5
gcloud beta builds triggers create cloud-source-repositories \
--name=<TRIGGER_NAME> \
--repo=<REPO_NAME> \
--branch-pattern=<BRANCH_PATTERN> \
--build-config=<CLOUDBUILD_YAML_FILEPATH>

connect repo


Check the trigger

After creating a trigger, we should see the trigger is there properly.

trigger check


Setup Cloud Build file

This is the main part of CI/CD. Each CI/CD tool uses their own format to work, like this Google Cloud Build.

Start from steps key, followed by a list.

One item in the list should have name of the image name, and args for the image arguments.

For example above, there are 2 steps

  1. The step has id “check-json”. This step is based on the image name “gcloud”.
    It waitFor nothing, yes the step can be executed immediately. Once the image is loaded and ready, we build it and access entrypoint “bash” and supply the args that “update, install ‘jq’, and execute ‘jq empty’ to check if any JSON files are incorrect”
  2. This step’s id is “copy-json”. It runs on the image name “gsutil”.
    This waitFor the step “check-json” to be completed first. Once the image is ready, we command it to copy (“cp”) to the bucket through its args.

For all available key and schema, please follow this link.


Make a commit and push

Once push completed and wait for some time, we can see if the build is successful or not. If not, we can amend our code and push again.

push

If using the app, we can check the build via the Github checks page by clicking the blue tick (or red cross if failed).

github checks

It will redirect to checks view.

check view


Verify the build result

Okay, the build is successful and we can check if our deployment is successful too.

check result

Yes, the files are there. The quest is completed.


Repo

The source code can be found here.

This post is licensed under CC BY 4.0 by the author.