-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
executable file
·98 lines (77 loc) · 2.64 KB
/
build.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
#! /usr/bin/env node
import fs from 'node:fs/promises';
import CP from 'child_process';
process.env.GITSHA = sha();
for (const env of ['GITSHA', 'AWS_REGION', 'AWS_ACCOUNT_ID']) {
if (!process.env[env]) {
console.error(`${env} Env Var must be set`);
process.exit();
}
}
await login();
if (!process.argv[2]) {
console.error('ok - building all containers');
await api();
for (const dir of await fs.readdir(new URL('./tasks/', import.meta.url))) {
await task(dir);
}
} else {
if (process.argv[2] === 'api') {
await api();
} else {
await task(process.argv[2]);
}
}
function login() {
return new Promise((resolve, reject) => {
const $ = CP.exec(`
aws ecr get-login-password \
--region $\{AWS_REGION\} \
| docker login \
--username AWS \
--password-stdin "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com"
`, (err) => {
if (err) return reject(err);
return resolve();
});
$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});
}
function api() {
return new Promise((resolve, reject) => {
const $ = CP.exec(`
docker compose build api \
&& docker tag cloudtak-api:latest "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{GITSHA\}" \
&& docker push "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{GITSHA\}"
`, (err) => {
if (err) return reject(err);
return resolve();
});
$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});
}
async function task(task) {
process.env.TASK = task;
return new Promise((resolve, reject) => {
const $ = CP.exec(`
docker buildx build ./tasks/$\{TASK\}/ -t cloudtak-$\{TASK\} \
&& docker tag cloudtak-$\{TASK\}:latest "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{TASK\}-$\{GITSHA\}" \
&& docker push "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{TASK\}-$\{GITSHA\}"
`, (err) => {
if (err) return reject(err);
return resolve();
});
$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});
}
function sha() {
const git = CP.spawnSync('git', [
'--git-dir', new URL('.git', import.meta.url).pathname,
'rev-parse', 'HEAD'
]);
if (!git.stdout) throw Error('Is this a git repo? Could not determine GitSha');
return String(git.stdout).replace(/\n/g, '');
}