Skip to content

Instantly share code, notes, and snippets.

@popeating
Created July 11, 2023 08:25
Show Gist options
  • Save popeating/208bcb78083b35513dcccec4ad67acf7 to your computer and use it in GitHub Desktop.
Save popeating/208bcb78083b35513dcccec4ad67acf7 to your computer and use it in GitHub Desktop.
webhook
const secret = "very_secret_key";
const repo = "/var/www/example.com";
const http = require('http');
const crypto = require('crypto');
const exec = require('child_process').exec;
const BUILD_CMD = 'npm install && NODE_ENV=production npm run build';
const PM2_CMD = 'pm2 restart <NODE APP NAME>';
http.createServer(function (req, res) {
req.on('data', function(chunk) {
let sig = "sha1=" + crypto.createHmac('sha1', secret).update(chunk.toString()).digest('hex');
if (req.headers['x-hub-signature'] == sig) {
exec('cd ' + repo + ` && git pull && npm install && ${BUILD_CMD} && ${PM2_CMD}`);
}
});
res.end();
}).listen(8200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment