-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
146 lines (132 loc) · 3.89 KB
/
deploy.js
File metadata and controls
146 lines (132 loc) · 3.89 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
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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const childProcess = require("child_process");
const force = require("express-force-domain");
const axios = require("axios");
const config = require("./config.json");
app.use(bodyParser.json({ limit: "50mb" })); // for parsing application/json
if (!config.dev) app.use(force(`http://${config.domain}:${config.port || 3000}`)); // For setting domain
app.post("/webhooks/gitlab", (req, res) => {
if (req.get("X-Gitlab-Token") !== config.secret) return res.sendStatus(401);
let projectFound = false;
let deployError = null;
// Loop through the projects
for (let i = 0; i < config.projects.length; i++) {
// Found a project with same repo name
if (
req.body.project.path_with_namespace ==
`${config.username}/${config.projects[i].repoName}`
) {
// Check if the project allows merge requests
if (req.body.event_name !== "push")
return res.status(406).send({ error: "Not acceptable event" });
if (req.body.object_kind === "push") {
projectFound = true;
// Push was to the given branch
if (req.body.ref == `refs/heads/${config.projects[i].branch}`) {
console.log(
`Project Found: ${config.projects[i].repoName} on ${
config.projects[i].branch
} branch, Starting auto deployment`
);
deploy(res, config.projects[i])
.then(result => {
discordWebhook(config.projects[i], true);
})
.catch(error => {
discordWebhook(config.projects[i], false);
deployError = error;
console.log("[ERROR] ", error);
});
if (deployError !== null) return res.status(500).send(deployError);
return res.status(200).send({
error: false,
message: `Successfully deployed ${config.projects[i].repoName}`
});
}
return res.status(406).send({ error: "Not acceptable branch" });
}
}
}
return res.status(404).send({ error: "Project not found" });
});
app.post("/redeploy", (req, res) => {
if (req.body.AccessToken !== config.secret) return res.sendStatus(401);
for (let i = 0; i < config.projects.length; i++) {
// Found a project with same repo name
if (req.body.repo == config.projects[i].repoName) {
console.log(
`Project Found: ${
config.projects[i].repoName
} on master branch, Starting auto deployment`
);
deploy(res, config.projects[i]).then(
result => {
return res.status(200).send({
error: false,
message: `Successfully deployed ${req.body.repo}`,
out: result
});
},
error => {
console.log("[ERROR] " + error);
return res.status(500).send(error);
}
);
}
}
return res.status(404).send({
error: "Project not found",
data: { repoName: req.body.repo }
});
});
const deploy = (res, project) => {
return new Promise(function(resolve, reject) {
childProcess.exec(
`cd ${config.deployerDir} && bash ./deployment_scripts/${project.scriptName}`,
function(err, stdout, stderr) {
if (err) {
reject(stderr);
}
console.log(stdout);
resolve(stdout);
}
);
});
};
const discordWebhook = (project, success) => {
let payload = null;
if (success) {
payload = {
username: "AutoDeployer - Server 01",
embeds: [
{
title: "AutoDeployer Status Update",
description: `***${project.repoName}*** has been successfully deployed`,
color: 8311585
}
]
};
} else {
payload = {
username: "AutoDeployer - Server 01",
embeds: [
{
title: "AutoDeployer Status Update",
description: ` An error occurred whilst trying to deploy ***${
project.repoName
}***. Check server console for more info. `,
color: 13632027
}
]
};
}
axios.post(config.webhook, payload).catch(e => {
console.log(JSON.stringify(e.response.data));
});
return;
};
app.listen(config.port || 3000, () =>
console.log(`Git Auto Deployer listening on port ${config.port || 3000}!`)
);