Created
January 10, 2020 02:50
-
-
Save jackpittenger/2532ee7353a99da85f74e50c93c72e05 to your computer and use it in GitHub Desktop.
Example webhook for processing on-push updates from GitHub in Node.js
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
//github.com/realSaddy | |
// This uses pm2 by default, feel free to change any of the pipeline commands | |
const secret = ""; | |
const repo = "/location"; | |
const branch = "master" | |
const pipeline = [ | |
'pm2 stop server', | |
'cd ' + repo + ' && git pull origin master', | |
`cd `+repo+` && npm install`, | |
`cd `+repo+` && pm2 restart server` | |
] | |
const http = require("http"); | |
const crypto = require("crypto"); | |
const execa = require('execa'); | |
const {signer} = require("x-hub-signature"); | |
http.createServer(function (req, res) { | |
var body = ""; | |
var sig = "" | |
req.on('data', function(chunk) { | |
console.log("data retrieved") | |
body += chunk; | |
sig = 'sha1=' + crypto.createHmac('sha1', secret).update(chunk).digest('hex') | |
}).on("end", async function(){ | |
try { | |
console.log("Request Recieved!") | |
let json = JSON.parse(body) | |
if (req.headers['x-hub-signature'] == sig && json["ref"] == "refs/heads/"+branch) { | |
console.log("Updating Repo "+repo) | |
for (let i = 0; i < pipeline.length; i++){ | |
const {stdout,stderr} = await execa.shell(pipeline[i]) | |
console.log(pipeline[i]+` stdout: `+stdout) | |
console.log(pipeline[i]+` stderr: `+stderr) | |
} | |
} else {console.log("Secret incorrect");} | |
} catch(err) { | |
console.error("Error: "+err); | |
} | |
}) | |
res.end(); | |
}).listen(8009); | |
console.log("on"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment