-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-server.js
More file actions
67 lines (56 loc) · 2.19 KB
/
git-server.js
File metadata and controls
67 lines (56 loc) · 2.19 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
/**
* Simple script for listening for web hooks from github and enable
* automatic deployment of builds for self-hosted websites and services!
*/
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var exec = require('child_process').execSync;
var config = require('./config');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
/**
* Web hook endpoint
*/
app.post('/git', function (req, res) {
// Just to keep github happy, we will always respond with a 200
res.sendStatus(200);
console.log(`Recieving web hook for the ${req.body.repository.name} repository!`);
if(!config.hasOwnProperty(req.body.repository.name)){
console.log("No config data found for this repository. Aborting")
return;
}
var repository = config[req.body.repository.name];
console.log("Pulling files");
// Reset any changes that have been made or added locally
exec(`git -C ${repository["localPath"]} reset --hard`, {stdio:[0,1,2]} );
exec(`git -C ${repository["localPath"]} clean -df`, {stdio:[0,1,2]});
exec(`git -C ${repository["localPath"]} pull -f`, {stdio:[0,1,2]});
if(needNPMInstall(req.body.commits)){
console.log("Need to install packages, this can take some time.");
exec(`npm -C ${repository["localPath"]} install `, {stdio:[0,1,2]});
}else{
console.log("Didn't need to install any packages");
}
console.log("Building distribution files");
exec(`npm -C ${repository["localPath"]} ${repository["buildCommand"]}`, {stdio:[0,1,2]});
if(repository.hasOwnProperty("serviceWorkerFile")){
console.log("Run service worker setup script");
exec(`node ${repository["localPath"]}/${repository["serviceWorkerFile"]}`, {stdio:[0,1,2]});
}
console.log("Copy all distribution files to destination");
exec(`sudo cp -v -a ${repository["localPath"]}/${repository["distributionFolder"]}/* ${repository["destinationPath"]}`, {stdio:[0,1,2]});
});
app.listen(config["port"], function () {
console.log(`Listening for incoming git hooks on port ${config["port"]}`)
});
function needNPMInstall(commits){
if(commits.length>0){
for(i in commits){
if(commits[i].modified.indexOf("package.json") > -1){
return true;
}
}
}
return false;
}