Skip to content

Instantly share code, notes, and snippets.

@nipeshkc7
Last active October 30, 2020 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nipeshkc7/37e70e104c658b1d5b331ccd85cc1775 to your computer and use it in GitHub Desktop.
Save nipeshkc7/37e70e104c658b1d5b331ccd85cc1775 to your computer and use it in GitHub Desktop.
cloud-build-slack-notifier
const IncomingWebhook = require('@slack/client').IncomingWebhook;
const SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/abc/xyz/jpt"
const humanizeDuration = require('humanize-duration');
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
// subscribe is the main function called by Cloud Functions.
module.exports.subscribe = (event) => {
const build = eventToBuild(event.data);
// Add additional statuses 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) {
console.log(`No included status ${build.status}`);
return;
}
// Send message to Slack.
const message = createSlackMessage(build);
webhook.send(message, (err, res) => {
if (err) console.log('Error:', err);
});
};
// eventToBuild transforms pubsub event message to a build object.
const eventToBuild = (data) => {
return JSON.parse(Buffer.from(data, 'base64').toString());
}
const DEFAULT_COLOR = '#4285F4'; // blue
const STATUS_COLOR = {
'QUEUED': DEFAULT_COLOR,
'WORKING': DEFAULT_COLOR,
'SUCCESS': '#34A853', // green
'FAILURE': '#EA4335', // red
'TIMEOUT': '#FBBC05', // yellow
'INTERNAL_ERROR': '#EA4335', // red
};
// createSlackMessage creates a message from a build object.
const createSlackMessage = (build) => {
let message = {
text: `Build ${build.id} finished `,
mrkdwn: true,
attachments: [
{
color: STATUS_COLOR[build.status] || DEFAULT_COLOR,
title: 'Build logs',
title_link: build.logUrl,
fields: [{
title: 'Status',
value: build.status
}, {
title: 'Duration',
value: humanizeDuration(new Date(build.finishTime) - new Date(build.startTime))
}],
footer: 'Google Cloud Container Builder',
footer_icon: 'https://ssl.gstatic.com/pantheon/images/containerregistry/container_registry_color.png',
ts: Math.round(new Date(build.finishTime).getTime()/1000)
}
]
};
// Add source information to the message.
let subs = build.substitutions || null;
console.log(`build: `, build);
if (subs) {
message.attachments[0].fields.push({
title: 'Repository',
value: subs.REPO_NAME
});
message.attachments[0].fields.push({
title: 'Branch - commit SHA',
value: `${subs.BRANCH_NAME} - ${subs.SHORT_SHA}`
});
}
// Add images to the message.
let images = build.images || [];
for (let i = 0, len = images.length; i < len; i++) {
message.attachments[0].fields.push({
title: 'Image',
value: images[i]
});
}
return message
}
{
"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",
"humanize-duration": "3.10.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment