-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (39 loc) · 1.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var http = require("http"),
request = require("request"),
createHandler = require("github-webhook-handler"), // module to do the dirty job
env = require("./env.js"), // environment functions
extensions = require("./extensions.js"), // extended prototypes
payload = require("./ghpayload.js"); // github payload helper
var secretKey = env.get("SECRET_KEY"), // github secret key
circleToken = env.get("CIRCLECI_TOKEN"), // circle-ci integration token
circleEndpoint = "https://circleci.com/api/v1",// circle-ci endpoint
projectName = env.get("PROJECT_NAME"), // project name i.e: user/project
branches = ["master", "staging"], // available branches for deploy
port = parseInt(env.get("PORT")); // port retrieved from Heroku env
var handler = createHandler({ path: "/deploy", "secret": secretKey});
http.createServer(function(req, res){
handler(req, res, function(err) {
res.statusCode = 404;
res.end("Not found!");
});
}).listen(port);
// Webhook Handlers
handler.on("error", function(err) {
console.error("Error: " + err);
});
handler.on("push", function(evt) {
var branch = null;
if(evt && evt.payload && evt.payload.ref) {
branch = payload.push.getBranch(evt.payload);
} else {
throw new Error("Received invalid payload!");
}
console.log("Received push on branch " + branch);
if(branches.contains(branch)) {
console.log("Triggering '"+branch+"' build...");
request.post(circleEndpoint + "/project/contentools/platform-build/tree/" + branch + "?circle-token=" + circleToken);
} else {
console.log("Nothing to do with the given branch.");
}
return "Done.";
});