-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (109 loc) · 2.76 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var http = require('http');
var Emitter = require('events').EventEmitter;
var helper = require('./lib/helper');
var exec = require('child_process').exec;
var fs = require('fs');
function GithubDeploy(){
var defaultConfig = {
/**
* route
* @type {Object} the key is github repo url, the value is directory to deploy the code on your machine
* example
*
* {
* 'STU-Fudan/testDeploy': '/home/stu/www/test1',
* 'STU-Fudan/test': '/home/stu/www/test2'
* }
*/
route: {},
port: 2333
};
this.config = defaultConfig;
this.init();
}
// simplify the POST data from github, only extract what I need
GithubDeploy.prototype.githubEventHookDataSimplify = function(githubEventHookData){
var githubEventHookData = githubEventHookData || {};
var rRef = /refs\/(heads|tags)\/(.*)$/;
var refMatches = (githubEventHookData.ref || '').match(rRef);
var branch = '';
var tag = '';
if (refMatches){
switch (refMatches[1]){
case 'heads':
branch = refMatches[2];
break;
case 'tags':
tag = refMatches[2];
break;
default:
}
}
var repo = githubEventHookData.repository.url || '';
var rRepo = /https:\/\/github.com\/(.*)$/;
repo = repo.match(rRepo)[1];
var ret = {
branch: branch,
tag: tag,
repo: repo
}
return ret;
}
GithubDeploy.prototype.init = function(){
var self = this;
function serverHandler(req, res){
var dataRaw = '';
if (req.method === 'POST'){
req.on('data', function(chunk){
dataRaw += chunk;
});
req.on('end', function(){
res.end();
dataRaw = JSON.parse(dataRaw);
var data = self.githubEventHookDataSimplify(dataRaw);
if (data.branch === 'publish'){
self.handle(data);
}
});
}
}
self.server = http.createServer(serverHandler);
}
GithubDeploy.prototype.handle = function(data){
var route = this.config.route,
repo;
for (repo in route){
if (data.repo === repo){
this.deploy(route[repo]);
return true;
}
}
console.warn('webhook [' + data.repo + '] not correctly routed!\n');
return false;
}
GithubDeploy.prototype.deploy = function(directory){
fs.exists(directory, function(exists){
console.log(exists, directory);
if (exists){
exec('cd ' + directory + ' && git pull && git checkout publish', function(error, stdout, stderror){
if (error){
console.log(error);
}
});
}
});
}
GithubDeploy.prototype.configure = function(config){
this.config = helper.merge(this.config, config);
return this;
}
GithubDeploy.prototype.run = function(){
if (helper.isEmptyObject(this.config.route)){
throw 'route must not be empty!';
}
this.server.listen(this.config.port, '127.0.0.1');
console.log('auto deploy server is now running at port ' + this.config.port + ' ... \n');
}
module.exports.factory = function(){
return new GithubDeploy();
}