Last active
November 27, 2018 21:01
-
-
Save nathanborror/7be8e97d015387ba79a3c9ce0229295e to your computer and use it in GitHub Desktop.
Slack webhook Cloud Functions for Google Cloud Container Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const IncomingWebhook = require("@slack/client").IncomingWebhook | |
const SLACK_WEBHOOK_URL = "SLACK_WEBHOOK_URL" | |
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL) | |
// subscribe is the main function called by Cloud Functions. | |
module.exports.slackSubscribe = (event, callback) => { | |
const build = eventToBuild(event.data.data) | |
// Skip if the current status is not in the status list. | |
// Add additional statues to list if you'd like: | |
// QUEUED, WORKING, SUCCESS, FAILURE, | |
// INTERNAL_ERROR, TIMEOUT, CANCELLED | |
const status = ["SUCCESS", "FAILURE", "INTERNAL_ERROR", "TIMEOUT"] | |
if (status.indexOf(build.status) === -1) { | |
return callback() | |
} | |
// Send message to Slack. | |
const message = createSlackMessage(build) | |
webhook.send(message, callback) | |
} | |
// eventToBuild transforms pubsub event message to a build object. | |
const eventToBuild = data => { | |
return JSON.parse(new Buffer(data, "base64").toString()) | |
} | |
const buildColor = status => { | |
switch (status) { | |
case "SUCCESS": | |
return "good" | |
case "FAILURE": | |
return "danger" | |
case "INTERNAL_ERROR": | |
return "danger" | |
default: | |
return "#999999" | |
} | |
} | |
// createSlackMessage create a message from a build object. | |
const createSlackMessage = build => { | |
var message = { | |
unfurl_links: false, | |
unfurl_media: false | |
} | |
if (build.status == "SUCCESS") { | |
message.text = `Service was deployed: <${ | |
build.logUrl | |
}|${build.sourceProvenance.resolvedRepoSource.commitSha.substring(0, 7)}>` | |
} else { | |
message.text = `Failed to deploy: <${ | |
build.logUrl | |
}|${build.sourceProvenance.resolvedRepoSource.commitSha.substring(0, 7)}>` | |
} | |
return message | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "google-container-slack", | |
"version": "0.0.1", | |
"description": | |
"Slack integration for Google Cloud Container Builder, using Google Cloud Functions", | |
"main": "index.js", | |
"dependencies": { | |
"@slack/client": "3.9.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Setup Slack Webhook | |
0/ Create a new Slack app: "YOUR_APP_NAME Deploy" | |
https://api.slack.com/apps?new_app=1 | |
1/ Add 'Incoming Webhook' and Activate it. Then click 'Add New Webhook to Workspace' | |
and pick the channel you want to post to. Copy the webhook URL to use in step 4. | |
2/ Create Cloud Storage bucket: | |
$ gsutil mb gs://[STAGING_BUCKET_NAME] Creating | |
gs://[PROJECT-ID]_cloudbuilds/[STAGING_BUCKET_NAME]... | |
3/ Create a directory on local system for the function code: | |
$ mkdir ~/gcb_slack $ cd ~/gcb_slack | |
4/ Add files to directory: index.js and package.json. Replace [SLACK-WEBHOOK] | |
with the Slack webhook URL. | |
5/ Deploy the subscribe function with a Cloud Pub/Sub trigger, run the following | |
command in the gcb_slack directory: | |
$ gcloud beta functions deploy slackSubscribe --stage-bucket [STAGING_BUCKET_NAME] --trigger-topic cloud-builds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment