Skip to content

Instantly share code, notes, and snippets.

@philographer
Last active April 17, 2017 17:17
Show Gist options
  • Save philographer/a0730fa7f706391ba6dbadda70f302c1 to your computer and use it in GitHub Desktop.
Save philographer/a0730fa7f706391ba6dbadda70f302c1 to your computer and use it in GitHub Desktop.
Github hook node.js

Prerequisites

$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo apt-get install npm
$ sudo npm install -g pm2

1. Open 3100 port on nginx
2. make a hook.sh like https://gist.github.com/yoohoogun114/f20e95eb38c7ab4f7982037d5deef84c
ref: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!! Notice !!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3. set ENV variable like this
export SECRET_TOKEN='토큰토큰토큰토큰'

Hook.js

/* 
 ** Need to set SECRET_TOKEN environment variable
 */
var http = require('http');
var fs = require('fs');
var crypto = require('crypto');
var hmac,calculatedSignature;
var exec = require('child_process').exec;
var cmd = './hook.sh';

server = http.createServer( function(req, res) {
    if (req.method == 'POST') {
        console.log("POST Reveived");
        var body = '';
        req.on('data', function (data) {
            body += data;
            //console.log("Partial body: " + body);
        });
        req.on('end', function () {
            //console.log("Body: " + body);
            console.log('this SECRET_TOKEN is ' + process.env['SECRET_TOKEN']);
            hmac = crypto.createHmac('sha1', process.env['SECRET_TOKEN']);
            console.log('SECRET_TOKEN is' + process.env['SECRET_TOKEN']);
            hmac.update(body);
            calculatedSignature = 'sha1=' + hmac.digest('hex');

            if (req.headers['x-hub-signature'] === calculatedSignature) {
                //console.log('all good');
                exec(cmd, function(error, stdout, stderr) {
                    console.log("=========hook.sh=======");
                    console.log(stdout);
                    console.log("=========hook.sh=======");
                    if(error) {
                        console.log("==========err=========");
                        console.log(error);
                        console.log("==========err=========");
                    }
                    if(stderr) {
                        console.log("==========stderr=========");
                        console.log(stderr);
                        console.log("==========stderr=========")
                    }
                });
            } else {
                console.log('not good');
                //console.log('want to is ' + req.headers['x-hub-signature']);
                console.log('but got is ' + calculatedSignature)
            }
        });
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('post received');
    } else if (req.method == 'GET') {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('get received');
    }
});

port = 3100;
host = 'localhost';
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment