Send Google Cloud Build notifications to Slack using Google Cloud Functions.
Before using google-cloud-build-slack-webhook, you'll need to have a few things squared away:
- a Google Cloud account,
- an application built with Google Cloud Build,
- a Slack workspace (with permissions to create Incoming Webhooks),
- the Google Cloud SDK installed on your cmoputer,
- Node.js installed on your computer, and
- comfort with the command line.
Note: For this walkthrough, we'll use indieweb-endpoints-cc
as the Google Cloud project ID. You'll want to substitute that value with your own project's ID.
With Node.js installed, clone this repository and run npm install
to install the project's dependencies.
Note: Give the Google Cloud Build documentation article Configuring notifications for third-party services a read. It's a solid rundown of the steps necessary to set up notifications to Slack using webhooks. The following are a few customized steps for deploying google-cloud-build-slack-webhook.
First, follow the steps outlined in the documentation linked above to create a new Incoming Webhook in your Slack workspace. With the webhook's URL, create a .env
file in the root of this project:
cp .env{.sample,}
Populate the .env
file with your project's GitHub repository URL and your newly-created Slack webhook URL:
GITHUB_REPO_URL=https://github.com/jgarber623/indieweb-endpoints.cc
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx
Using the Google Cloud SDK, set the current project:
gcloud config set project indieweb-endpoints-cc
Next, export a unique BUCKET_NAME
variable. This will be used when creating and interacting with a bucket on Google Cloud Storage. Bucket names must be globally unique (thus the date | md5 | cut -c -10
bits borrowed from Philippe Modard's setup.sh
script).
export BUCKET_NAME=$(gcloud config get-value project)_cloudbuild_webhook_$(date | md5 | cut -c -10)
Note: You'll want to keep this bucket name handy for future command line sessions. Keep it somewhere safe!
Use gsutil
to create the bucket on Google Cloud Storage:
gsutil mb gs://$BUCKET_NAME
You should now be able to deploy to Google Cloud Functions:
npm run deploy
If all goes well, you'll now receive notifications of Slack when Google Cloud Build runs on your project!
The following documentation and projects served as guides and inspiration for the development of google-cloud-build-slack-webhook:
- Google Cloud Build documentation's Configuring notifications for third-party services
- Google Cloud Build's Build Resource documentation
- Slack's @slack/webhook Node.js module
- Slack's Messaging for Slack apps documentation
- Philippe Modard's google-cloud-build-slack project
google-cloud-build-slack-webhook is written and maintained by Jason Garber.
google-cloud-build-slack-webhook is freely available under the MIT License. Use it, learn from it, fork it, improve it, change it, tailor it to your needs.