Skip to content

Instantly share code, notes, and snippets.

@andrewodri
Last active December 1, 2021 21:29
Show Gist options
  • Save andrewodri/f00dc7bc5949dbe65b6e26a2c1d89a06 to your computer and use it in GitHub Desktop.
Save andrewodri/f00dc7bc5949dbe65b6e26a2c1d89a06 to your computer and use it in GitHub Desktop.
Docker Hub to Slack Incoming Webhook
const fs = require('fs');
const https = require('https');
const querystring = require('querystring');
const url = require('url');
const util = require('util');
let app = function(req, res) {
if(req.method.toLowerCase() == 'post'){
let dockerBody = '';
let dockerUrl;
let dockerData;
let slackBody = '';
let slackData;
req.on('data', chunk => dockerBody += chunk).on('end', () => {
dockerUrl = url.parse(req.url);
dockerData = Object.assign(
JSON.parse(dockerBody),
querystring.parse(dockerUrl.query)
);
if(dockerData.include && dockerData.include !== dockerData.repository.repo_name) return;
if(dockerData.exclude && dockerData.exclude == dockerData.repository.repo_name) return;
//console.log(util.inspect(dockerData, { color: true, depth: null }));
let statusText = dockerData.repository.status.toLowerCase();
let statusColor;
if(statusText == 'success') statusColor = 'good';
else if(statusText == 'error') statusColor = 'danger';
slackData = {
"icon_emoji": ":docker:",
"attachments": [
{
"color": statusColor,
"title": util.format("Docker Hub Build %s", dockerData.repository.status),
"title_link": dockerData.repository.repo_url + "builds/",
"text": util.format("The automated build status for the repo *%s* is *%s*.", dockerData.repository.repo_name, statusText),
"mrkdwn_in": [ "text" ]
}
]
};
//console.log(util.inspect(slackData, { color: true, depth: null }));
let slackReq = https.request({
hostname: 'hooks.slack.com',
port: 443,
path: dockerUrl.pathname,
method: 'post',
headers: {
'Content-Type': 'application/json'
}
}, slackRes => {})
.end(JSON.stringify(slackData));
});
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(slackData));
}
}
let readFile = fileName => new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if(err){ reject(err); return; }
resolve(data.toString());
});
});
Promise.all([
readFile(process.argv[2]),
readFile(process.argv[3]),
]).then(values => {
let server = https.createServer({
key: values[0],
cert: values[1],
}, app).listen(443);
}).catch(reason => {
console.log(reason);
});
@andrewodri
Copy link
Author

andrewodri commented Jan 6, 2017

You can configure this webhook by following these steps:

  1. Create an incoming webhook in Slack (e.g. https://hooks.slack.com/TXXXXX/BXXXXX/XXXXXXXXXX)
  2. Replace the domain portion of the webhook with domain of the server (e.g. http://s3-website-us-east-1.amazonaws.com/TXXXXX/BXXXXX/XXXXXXXXXX)
  3. Add the query parameter include to the URL with the value being the repo you want to filter (e.g. http://s3-website-us-east-1.amazonaws.com/TXXXXX/BXXXXX/XXXXXXXXXX?include=mogo/ui_core)
  4. Add this URL as a Docker Hub webhook

You can execute this application by following these steps:

  1. Run node app.js /path/to/private/key.key /path/to/ssl/certificate.crt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment