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.
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.
This is the build history. All builds can be viewed here.
And this is the trigger page. All triggers can be seen and we can manage like create, delete, update, and disable on this page.
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!
Let's give a story, we want to upload validated JSON files from our repo to Google Cloud Storage. The steps should be:
- Verify all JSON files are correct
- 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) Connect Repository
(2) Authenticate and install app
(3) Install on Github page
(4) 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.
(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.
(2) Connect external repository
(3) Authorize to Github
(4) Select a repo
(5) Mirror completed
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
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.
2. Connect repo through mirroring
gcloud beta builds triggers create cloud-source-repositories \ --name=<TRIGGER_NAME> \ --repo=<REPO_NAME> \ --branch-pattern=<BRANCH_PATTERN> \ --build-config=<CLOUDBUILD_YAML_FILEPATH>
Check the trigger
After creating a trigger, we should see the trigger is there properly.
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.
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
- The step has
id"check-json". This step is based on the image
waitFornothing, yes the step can be executed immediately. Once the image is loaded and ready, we build it and access
entrypoint"bash" and supply the
argsthat "update, install 'jq', and execute 'jq empty' to check if any JSON files are incorrect"
- This step's
idis "copy-json". It runs on the image
waitForthe step "check-json" to be completed first. Once the image is ready, we command it to copy ("cp") to the bucket through its
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.
If using the app, we can check the build via the Github checks page by clicking the blue tick (or red cross if failed).
It will redirect to checks view.
Verify the build result
Okay, the build is successful and we can check if our deployment is successful too.
Yes, the files are there. The quest is completed.
The source code can be found here.