If your team is using Google Cloud Build for CI (Continuous Integration) process and is a client of Google Workspace (a.k.a G-Suite). We can setup a bot to send message when the build is finished.
This tutorial requires basic of Python.
1. Google Cloud Build
First of all is, we must have Google Cloud Build triggers (doc). If yes, go next.
2. Google Cloud Pub/Sub
There is a topic of Cloud Pub/Sub named “cloud-build” automatically when we have triggers. The message will be in this format.
3. Google Chat
Next is to create a room of Google Chat and an incoming webhook by this link.
4. Google Cloud Functions
After all, it’s our turn to create a Google Cloud Functions to connect to the Pub/Sub topic with a condition to send the pushed messages to the Google Chat room through the webhook.
4.1 Write a function to handle messages from Pub/Sub
With these lines, the Google Cloud Functions will receive messages from a specific Pub/Sub topic.
A message comes into variable
event. We extract the byte array in path “data” then decode and transform to json format. Now we have payload in variable
4.2 Create a function to send a Google Chat message
This doc explains us there are 2 types of Google Chat message that are texts and cards. Select cards to expose information in rich way.
These are my design.
- Display id of the build and branch in short format.
- Date and time from Pub/Sub message is in nanosecond format such as
"2021-02-19T06:28:04.329309285Z". We apply method
.isoparse() of library
dateutil to transform that string to
- Update timezone from UTC to “Asia/Bangkok” using the library
- Add links to the build log.
- Compute build duration since a start time of
"FETCHSOURCE" to an end time of
A time to call webhook is to use method
Http().request(). The body will be an properly encoded json string via
Here is an example card.
4.3 embed the URL to an environment variable
Hardcoding the URL is not a good idea. We choose to embed it as an environment variable called
"_URL" then refer it in code with the variable
GGCHAT_URL = os.environ.get('_URL', 'Environment variable does not exist')
# code ...
response = http_obj.request(
4.4 assemble all into
Calling the function
send_ggchat() in function
cloudbuild_notifications(). This is all of our function in the file “main.py”.
4.5 Create requirements.txt
This is because we have imported many external libraries. Need this to tell Google Cloud Functions to prepare the following library on deployment.
4.6 Prepare an environment variable file
Because we have environments variables, we also write this file as a reference.
Name this “env.yaml”.
4.7 Arrange files and folders
prepare files like this.
1 directory, 3 files
4.8 deploy function
To deploy via terminal/cmd, make sure Google Cloud SDK is already install in the machine (how to install). Go to the folder “source” and submit this command.
gcloud functions \
--project [project_id] deploy cloudbuild-notifications \
--entry-point cloudbuild_notifications \
--runtime python38 \
--env-vars-file ../env.yaml \
The “project_id” must be matched with the one of Pub/Sub or the function cannot be triggered.
4.9 Review it
These message are displayed mean the function is deployed successfully.
Here is the repo of this tutorial.