I have a blog about sending build results from Google Cloud Build to Google Chat (here). And this is a version for Slack.

In case you don't aware of, Slack is a popular messaging app among developer teams and companies with its feature-rich more than just texting between individuals and groups. It also offers APIs that developers love to use for their wonderful automated pipelines.

And Google Cloud Build is a main CI/CD tool in Google Cloud Platform. It's super easy to build your code into services in GCP.


Diagram


Services we need

  1. Google Cloud Build
  2. Google Cloud Pub/Sub topic cloud-builds
  3. Slack app or webhook API
  4. Google Secret Manager to store Slack API as a secret
  5. Google Cloud Functions as a main operator

Steps

1. Prepare Cloud Build

First of all, we need to enable Google Cloud Build API and Google Cloud Pub/Sub API. There must be one topic called cloud-builds and Google Cloud Build will automatically publish build events through that.

2. Prepare Slack webhook API

In this blog we will try Slack webhook API solution. Another is using Slack app.

We can follow the steps to create a webhook url in the link below.

Sending messages using incoming webhooks
Create an incoming webhook with a unique URL to which you send a JSON payload with message text and options.

We should get an webhook url in the "Incoming webhook" page in settings and it's supposed to be like this https://hooks.slack.com/services/ABC/DEF/GHIJ1234.

3. Store webhook in Google Secret Manager

We need to save it secret in Google Secret Manager and retrieve in Google Cloud Functions as an environment variable.

4. Design Slack message template

Go to Slack Block Kit. Here we can design our own style messages that we want to see in Slack channels.

Building with Block Kit
String the atoms together into molecules and inject them into messages and modals.

Here is my sample template for this operation.

5. Implement Google Cloud Functions

The last step is to implement the main Cloud Functions.

My Cloud Functions uses Python to receive messages from the topic cloud-builds which forwards from Google Cloud Build. Then extract the message and assemble to a new Slack message and send out through the Slack webhook API.

6. Deploy Google Cloud Functions

Just using the command to deploy directly.

gcloud functions deploy NAME [flags]

For full scripts, please review cloudbuild.yaml in the repo link at the bottom.


Real outputs

When everything is done, we should see the result like this.


Repo

GitHub - bluebirz/google-cloud-build-slack-python: Sending slack message for Google Cloud Build’s result
Sending slack message for Google Cloud Build’s result - bluebirz/google-cloud-build-slack-python