forked from joshbetz/node-github-auto-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·77 lines (65 loc) · 1.53 KB
/
Copy pathindex.js
File metadata and controls
executable file
·77 lines (65 loc) · 1.53 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
var fs = require('fs');
var child = require('child_process');
var github = require('githubhook')({
'path': '/',
'port': process.env.PORT,
'secret': process.env.SECRET,
'logger': console
});
function usage() {
console.log("Usage: %s </path/to/repo> [</path/to/post-deploy.sh>]", process.argv[1]);
process.exit(1);
}
var dir = process.argv[2];
if (!dir) {
usage();
}
fs.stat(dir, function(err, stats) {
// dir must exist
if (err) {
console.error(err);
usage();
}
// dir must be git repo
fs.stat(dir + '/.git', function(err, stats) {
if (err) {
console.error(dir, 'is not a git repo');
usage();
}
});
});
var script = process.argv[3];
// script must be executable
if (script) {
fs.stat(script, function(err, stats) {
if ( stats && ! (stats.mode & 0100) ) {
console.error(script, 'is not executable');
process.exit(1);
}
});
}
github.listen();
github.on('push', function(repo, ref, data) {
// TODO: Check repo is the repo in dir
var sha = data.after;
var command = 'git fetch && git checkout ' + sha;
child.exec(command, { cwd: dir }, function(err, stdout, stderr) {
if (err) {
console.error(err);
} else {
console.error(stderr);
console.log(stdout);
if (script) {
child.exec(script, { cwd: dir }, function(err, stdout, stderr) {
if (err) {
console.error(err);
} else {
console.error(stderr);
console.log(stdout);
}
});
}
}
});
});