Last active
August 25, 2016 18:44
-
-
Save rajatvig/93b3acb9cd76d100c32e18b58c01c9e5 to your computer and use it in GitHub Desktop.
JavaScript code to post Slack Notifications for GoCD Build Status
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
slack_passed: | |
BUILD_STATUS=passed node -pe "require('./slack')" | |
slack_failed: | |
BUILD_STATUS=failed node -pe "require('./slack')" |
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 https = require('https'); | |
const url = require('url'); | |
function getSlackPayload() { | |
let pipelineLink = '<https://' + process.env.GO_SERVER_URL + '/go/tab/pipeline/history/' + | |
process.env.GO_PIPELINE_NAME + '|' + | |
process.env.GO_PIPELINE_NAME + '>'; | |
let stageLink = '<https://' + process.env.GO_SERVER_URL + '/go/pipelines/' + | |
process.env.GO_PIPELINE_NAME + '/' + | |
process.env.GO_PIPELINE_COUNTER + '/' + | |
process.env.GO_STAGE_NAME + '/' + | |
process.env.GO_STAGE_COUNTER + '|' + | |
process.env.GO_STAGE_NAME + '>'; | |
let buildPassed = (process.env.BUILD_STATUS === 'passed'); | |
let iconEmoji = (buildPassed ? ':thumbsup:' : ':thumbsdown:'); | |
let textMessage = [ | |
process.env.GO_PIPELINE_NAME, | |
process.env.GO_PIPELINE_LABEL, | |
process.env.GO_STAGE_NAME, | |
process.env.GO_STAGE_COUNTER, | |
(buildPassed ? 'passed' : 'failed') | |
].join(' / '); | |
let notifyFields = [ | |
{ title: 'Pipeline', value: pipelineLink, short: true }, | |
{ title: 'Stage', value: stageLink, short: true }, | |
{ title: 'Label', value: process.env.GO_PIPELINE_LABEL, short: true}, | |
{ title: 'Commit', value: process.env.GO_REVISION, short: true }, | |
{ title: 'Triggered by', value: process.env.GO_TRIGGER_USER, short: true} | |
]; | |
return { | |
channel: process.env.SLACK_CHANNEL || '#eng-gocd-notify', | |
text: textMessage, | |
color: (buildPassed ? 'good' : 'danger'), | |
username: 'GoCD Build Bot', | |
icon_emoji: iconEmoji, | |
attachments: [ | |
{ | |
fallback: process.env.FALLBACK || '', | |
fields: notifyFields | |
} | |
] | |
}; | |
} | |
function makeRequest(postData) { | |
let slackUrl = url.parse(process.env.SLACK_URL); | |
let options = { | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(postData), | |
'Accept': 'application/json' | |
}, | |
method: 'POST', | |
hostname: slackUrl.host, | |
path: slackUrl.path | |
}; | |
let req = https.request(options, (res) => { | |
// console.log('statusCode: ', res.statusCode); | |
// console.log('headers: ', res.headers); | |
res.setEncoding('utf8'); | |
var body = ''; | |
res.on('data', (chunk) => { | |
body += chunk; | |
}); | |
res.on('end', () => { | |
// console.log('Processed HTTPS response from Slack'); | |
if (res.statusCode === 200) { | |
console.log('replied with ' + res.statusCode + ' for ' + slackUrl + ' with ' + body); //eslint-disable-line no-console | |
} else { | |
console.error('replied with ' + res.statusCode + ' for ' + slackUrl); //eslint-disable-line no-console | |
} | |
}); | |
}); | |
req.write(postData); | |
req.end(); | |
req.on('error', (err) => { | |
console.error(err); //eslint-disable-line no-console | |
console.error('request errored for ' + slackUrl); //eslint-disable-line no-console | |
}); | |
} | |
makeRequest(JSON.stringify(getSlackPayload())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then in GoCD tasks, add a task to call
make slack_passed