-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (131 loc) · 4.6 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
138
139
140
141
142
143
144
145
146
147
148
const { execSync, spawn } = require('child_process');
const crypto = require('crypto');
const http = require('http');
const fs = require('fs');
const path = require('path');
const CMD = require('./cmds');
const SECRET = process.env.SECRET || 'MY_GIT_WEBHOOK_SECRET';
/*
/* ############################################################/
/* ### Build command will run @ ${directory}/project.name/ ###/
/* ############################################################/
*/
const directory = process.env.HOME_DIR || '/home/deploy'
var current_file;
http
.createServer((request, response) => {
if (request.method === "GET") {
if (!current_file) {
response.writeHead(200);
response.end('ready');
return;
}
const filePath = path.join(directory, 'logs', current_file);
if (!fs.existsSync(filePath)) {
response.writeHead(200);
response.end('ready..' + filePath);
return;
}
const stat = fs.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': stat.size
});
fs.createReadStream(filePath).pipe(response);
return;
}
if (request.method !== "POST") {
response.writeHead(405, 'Method Not Supported', { 'Content-Type': 'text/html' });
response.end('<!doctype html><html><head><title>405</title></head><body>405: Method Not Supported</body></html>');
return;
}
const isAllowed = request.headers['x-gitlab-token'] === SECRET; //|| request.headers['x-hub-signature'] === signature
if (!isAllowed) {
response.writeHead(403, 'Authentication failed, Access Forbidden', { 'Content-Type': 'text/html' });
response.end('<!doctype html><html><head><title>403</title></head><body>403: Authentication failed, Access Forbidden</body></html>');
return;
}
var requestBody = '';
var signature;
request.on('data', function (data) {
signature = `sha1=${crypto
.createHmac('sha1', SECRET)
.update(data)
.digest('hex')}`;
requestBody += data;
if (requestBody.length > 1e7) {
response.writeHead(413, 'Request Entity Too Large', { 'Content-Type': 'text/html' });
response.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
}
});
request.on('end', function () {
const body = JSON.parse(requestBody);
const name = body.project.path_with_namespace;
const isMaster = body && body.ref === 'refs/heads/master';
const baseDir = path.join(directory, name);
const logFile = path.join(directory, 'logs', body.checkout_sha + '.log');
const cmd = CMD[name] ? CMD[name].cmd : ["yarn", "build"];
const cwd = CMD[name] ? CMD[name].cwd : ".";
current_file = body.checkout_sha;
if (fs.existsSync(logFile)) {
response.writeHead(409);
response.end('Skipping.. already deployed');
return;
}
if (current_file) {
response.writeHead(412);
response.end('Skipping.. another deployment already running');
return;
}
current_file = body.checkout_sha;
const out = fs.openSync(logFile, 'a');
const err = fs.openSync(logFile, 'a');
if (isMaster) {
/*
/* ############################################################/
/* ### RESET ANY CHANGES ON SERVER AND PULL LATEST VERSION ####/
/* ############################################################/
*/
fs.writeSync(out, 'building ' + baseDir + '...\n');
try {
execSync('git checkout master -f && git reset master --hard && git pull', {
cwd: baseDir,
timeout: 60 * 5 * 1000,
stdio: ['ignore', out, err]
})
} catch (e) {
fs.writeSync(err, JSON.stringify(error));
}
try {
/*
/* ############################################################/
/* ### EDIT THIS LINE IF YOU WANT TO CHANGE BUILD COMMAND #####/
/* ############################################################/
*/
fs.writeSync(out, cwd + ': running ' + cmd + '...\n');
const child = spawn(cmd[0], cmd.slice(1), {
detached: true,
cwd,
timeout: 60 * 5 * 1000,
stdio: ['ignore', out, err]
});
child.unref();
child.on('exit', (exitCode) => {
if (parseInt(exitCode) !== 0) {
//Handle non-zero exit
fs.writeSync(err, '\nxxxx..ERROR[' + exitCode + ']..xxxxx...\nbuild failed...\n');
}
fs.writeSync(out, '\nbuild success....\n');
});
} catch (error) {
fs.writeSync(err, JSON.stringify(error));
}
response.writeHead(201);
response.end(`Deployed ${body.commits[0].title} of ${body.project.path_with_namespace}`);
} else {
response.writeHead(200);
response.end('Skipping.. we only deploy master');
}
});
})
.listen(process.env.PORT || 9090);