Post

Slack me what you build

Slack offers APIs that developers love to use for their wonderful automated pipelines.

Slack me what you build

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

sequenceDiagram
  autonumber

  box Google Cloud Platform
  participant gcb as Google<br/>Cloud Build
  participant gps as Google<br/>Cloud Pub/Sub
  participant gcf as Google<br/>Cloud Functions
  participant gsm as Google<br/>Secret Manager
  end

  box Slack
  participant web as Slack<br/>Webhook
  participant chn as Slack<br/>Channel
  end

  gcb->>gps: send build events
  gps->>gcf: trigger eventarc
  gcf->>gsm: get Slack url
  activate gsm
  gsm->>gcf: return Slack url
  deactivate gsm
  gcf->>web: post Slack messages
  web->>chn: display messages
  

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.

topics

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.

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”.

webhook

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.

secret manager

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.

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.

1
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.

slack message


Repo

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