This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.js
62 lines (52 loc) · 1.62 KB
/
deploy.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
50
51
52
53
54
55
56
57
58
59
60
61
62
// Setup:
// Install git and node
// sudo npm install jitsu -g
// git clone https://github.com/OpenUserJs/OpenUserJS.org.git
// cd OpenUserJS.org
// jitsu login
// Copy this file into the directory
// node deploy.js &
var express = require('express');
var spawn = require('child_process').spawn;
var app = express();
var ghPath = 'OpenUserJs/OpenUserJS.org';
app.configure(function(){
app.use(express.urlencoded());
app.use(express.json());
app.use(express.methodOverride());
app.use(app.router);
});
app.listen(7070);
app.post('/', function (req, res) {
var payload = null;
var username = null;
var reponame = null;
var git = null;
var jitsu = null;
res.end(); // close connection
// Test for know GH webhook ips: https://api.github.com/meta
if (!req.body.payload ||
!/192\.30\.252\.(2[0-5][0-5]|1[0-9]{2}|[1-9]?\d)/
.test(req.headers['x-forwarded-for'] || req.connection.remoteAddress)) {
return;
}
payload = JSON.parse(req.body.payload);
// Only accept commits to the master branch
if (!payload || payload.ref !== 'refs/heads/master') { return; }
// Make sure we have the right repo
username = payload.repository.owner.name;
reponame = payload.repository.name;
if (ghPath === username + '/' + reponame) {
// git rest --hard
// git pull origin master
// jitsu deploy
git = spawn('git', ['reset', '--hard']);
git.on('close', function (code) {
git = spawn('git', ['pull', '-f', 'origin', 'master']);
git.on('close', function (code) {
jitsu = spawn('jitsu', ['-c', 'deploy']);
jitsu.on('close', function(code) { return; });
});
});
}
});