Skip to content

Instantly share code, notes, and snippets.

@greenlikeorange
Created October 20, 2015 07:40
Show Gist options
  • Save greenlikeorange/2814de78237180a3d808 to your computer and use it in GitHub Desktop.
Save greenlikeorange/2814de78237180a3d808 to your computer and use it in GitHub Desktop.
Automatic deployement -- Nodejs, Pm2 and github
var http = require("http");
var exec = require("child_process").exec;
var createHandler = require("github-webhook-handler");
var handler = createHandler({ path: "/", secret: "လျှို့ဝှက်ချက်"});
http.createServer(function(req, res) {
handler(req, res, function (err) {
res.statusCode = 404;
res.end("no such location");
});
}).listen(9999);
handler.on("push", function (event) {
var repo = event.payload.repository.name;
var comps = event.payload.ref.split("/");
if(comps[2] !== "master") {
console.log("Received a push on %s and no build has is triggered", comps[2]);
return;
}
console.log("Received a push on master, build started...");
exec("cd ~/" + repo + " && git pull origin master && npm install && pm2 restart " + repo, function(error, stdout, stderr) {
if(error != null) {
console.log("Error during the execution of redeploy: " + stderr);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment