Skip to content

Instantly share code, notes, and snippets.

@acclaim-tech
Created November 7, 2017 18:10
Show Gist options
  • Save acclaim-tech/193e48c64e18aca2c4d99fe613a2f159 to your computer and use it in GitHub Desktop.
Save acclaim-tech/193e48c64e18aca2c4d99fe613a2f159 to your computer and use it in GitHub Desktop.
simple serverless plugin to notify Slack channel about deployment
{
"name": "serverless-aci-slack-plugin",
"version": "1.0.0",
"description": "A plugin to notify slack channel when deployment is complete",
"author": "VB",
"license": "MIT",
"dependencies": {
"superagent": "^3.6.3",
"superagent-proxy": "^1.0.2",
"superagent-retry": "^0.6.0"
}
}
'use strict';
const spawnSync = require('child_process').spawnSync;
const request = require('superagent');
require('superagent-proxy')(request);
require('superagent-retry')(request);
class ServerlessPlugin {
constructor(serverless, options) {
//console.log('serverless-aci-slack-plugin instantiated');
this.serverless = serverless;
this.options = options;
this.commands = {
notifySlack: {
usage: 'Notifies slack channel after deployement',
lifecycleEvents: [
'push',
],
}
};
this.hooks = {
'after:deploy:deploy': this.notifySlack.bind(this)
};
}
notifySlack() {
//console.log('ready to push data in slack');
var endPoint ='https://hooks.slack.com/services/<XXXXXXX>';
var stackName = this.serverless.providers.aws.naming.getStackName(this.options.stage);
this.endpointInfo(function(reply){
var text = stackName + " deployed";
if (reply && reply.length>0) text += ". Service Endpoint: "+reply;
var notification = {"text": text};
var publishReq = request.post(endPoint);
if (process.env.http_proxy) {
publishReq = publishReq.proxy(process.env.http_proxy);
}
//console.log(notification);
publishReq.retry(3)
.send(notification)
.end(function (err, apiResponse) {
if (err) {
console.log(JSON.stringify(err));
}
});
});
}
endpointInfo(cb) {
const provider = this.serverless.getProvider('aws');
const stackName = provider.naming.getStackName(this.options.stage);
return provider
.request(
'CloudFormation',
'describeStacks',
{ StackName: stackName },
this.options.stage,
this.options.region
)
.then((result) => {
const outputs = result.Stacks[0].Outputs;
const output = outputs.find(entry => entry.OutputKey === 'ServiceEndpoint');
if (output.OutputValue) {
this.serverless.cli.log(`ServiceEndpoint: ${output.OutputValue}`);
cb(`${output.OutputValue}`);
} else {
this.serverless.cli.log('ServiceEndpoint: Not Found');
cb('');
}
});
}
}
module.exports = ServerlessPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment