-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
84 lines (64 loc) · 2.29 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
require('dotenv').config({path:'~/.thx-deploy/.env'});
const axios = require('axios');
const updater = require("@bugcrowd/ecs-service-image-updater");
const monitor = require("@bugcrowd/ecs-deployment-monitor");
const ecsTaskRunner = require("@bugcrowd/ecs-task-runner");
const DISCORD_WEBHOOK = process.env.DISCORD_WEBHOOK;
module.exports = function (app, config, tag) {
let statusCodes = {
taskUpdate: undefined,
taskRunner: undefined,
deployment: undefined,
};
let formerTaskDefinition;
const options = {
...config,
image: `${config.repository}:${tag}`,
};
updater.getServiceTaskDefinition(options, (err, taskDefinition) => {
formerTaskDefinition = taskDefinition;
});
updater(options, (err, taskDefinitionArn) => {
if (err) throw err;
console.log("Updated task definition");
updateStatus("taskUpdate", 0);
const ecsOptions = {
...options,
taskDefinitionArn,
};
ecsTaskRunner(ecsOptions, function (err, stream) {
if (err) throw err;
stream.on("error", (err) => {
throw err;
});
stream.pipe(process.stdout);
stream.on("end", () => {
console.log("Migration run with exitcode:", stream.logStream.exitCode);
updateStatus("taskRunner", parseInt(stream.logStream.exitCode));
});
});
let deployment = monitor(ecsOptions);
deployment.on("error", (error) => console.log("Deployment state:", error));
deployment.on("state", (state) => console.log("Deployment state:", state));
deployment.on("end", async () => {
const isFailure = deployment.isFailure()
const exitCode = await updateStatus("deployment", isFailure ? 1 : 0);
if (DISCORD_WEBHOOK) await updateDiscord(exitCode);
process.exit(exitCode);
});
});
async function updateDiscord(isFailed) {
const emoji = isFailed ? '⛔' : '✅';
const data = { content: `${ emoji } Released ${app} \`${ tag }\`` };
await axios({ url: DISCORD_WEBHOOK, method: "POST", data });
}
async function updateStatus(key, status) {
statusCodes[key] = status;
if (
Object.values(statusCodes).filter((v) => v === undefined).length === 0
) {
console.log("exitCodes", statusCodes);
return Object.values(statusCodes).filter((v) => v !== 0).length;
}
}
};