-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-n-deploy.js
70 lines (63 loc) · 2.32 KB
/
build-n-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
63
64
65
66
67
68
69
70
/*
This is a small nodejs server that serves as an interface for bitbucket POST hook.
Bitbucket expectation: http://rentever.com:8765/deploy-me-now
Upon hitting URL above script will simply execute ./deploy.sh. Results are logged in /var/log/build-n-deploy
/history - to see history of all builds
*/
var exec = require('child_process').exec,
http = require('http'),
fs = require('fs');
var logStash = '/var/log/build-n-deploy/';
function execute(command, callback) {
exec(command, function (error, stdout, stderr) {
if (error) {
callback(error);
}
if (stderr) {
callback(stderr);
}
callback(stdout);
});
}
http.createServer(function (req, res) {
if (req.url === '/deploy-me-now') {
execute('./deploy.sh', function (result) {
var filename = new Date().toISOString().replace(/T/, '-').replace(/\..+/, '');
fs.writeFile(logStash + filename, result, function (err) {
if (err) {
console.log(err);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(result.replace(/(?:\r\n|\r|\n)/g, '<br>'));
});
});
} else if (req.url === '/history') {
var html = '';
fs.readdir(logStash, function (err, files) {
if (err) {
console.log(err);
}
console.log(files.length);
if (files.length) {
files.forEach(function (item, index) {
if (item.indexOf('.')) {
html = (index + 1) + ': <a href="/file/' + item + '">' + item + '</a><br>' + html;
}
});
} else {
html = 'No history yet';
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});
} else if (req.url.indexOf('/file/') === 0) {
var filename = req.url.split('/')[2];
var result = fs.readFileSync(logStash + filename, { encoding: 'utf8' });
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(result.replace(/(?:\r\n|\r|\n)/g, '<br>'));
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('404 Not Found\n');
res.end();
}
}).listen(8765);