(Google) Chat me what you (tell Google Cloud) Build
Feb 21, 2021
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.
Overview
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” that we have to create if we don't have yet 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.
Let’s start.
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 message.
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 datetime.
Update timezone from UTC to “Asia/Bangkok” using the library pytz.
Add links to the build log.
Compute build duration since a start time of "FETCHSOURCE" to an end time of "BUILD".
A time to call webhook is to use method Http().request(). The body will be an properly encoded json string via json.dumps(json.loads()).
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".
import os
GGCHAT_URL = os.environ.get('_URL', 'Environment variable does not exist')
def send_ggchat(payload):
# code ...
response = http_obj.request(
uri=GGCHAT_URL,
method='POST',
headers=message_headers,
body=json.dumps(json.loads(message))
)
4.4 assemble all into main.py
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.
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.