forked from TMS-Uni-Stuttgart/Tutor-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-docker-image.ts
160 lines (122 loc) · 3.96 KB
/
build-docker-image.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as childProcess from 'child_process';
import { ExecSyncOptions } from 'child_process';
import * as fs from 'fs';
interface ArgumentOptions {
name: string;
short?: string;
hasValue?: boolean;
}
const DEFAULT_IMAGE_NAME = 'dudrie/tutor-management-system';
function getArgValue({ name, short, hasValue }: ArgumentOptions): string | undefined {
const args = process.argv;
const nameExpression = `--${name}${hasValue ? '=' : ''}`;
const shortExpression = short ? `-${short}${hasValue ? '=' : ''}` : undefined;
const argValue = args.find((arg) => {
if (arg.includes(nameExpression)) {
return true;
}
if (!!shortExpression && arg.includes(shortExpression)) {
return true;
}
return false;
});
if (!argValue) {
return undefined;
}
if (hasValue) {
const shortRegexPattern: string = shortExpression ? `|(${shortExpression})` : '';
const regex = new RegExp(`(${nameExpression})${shortRegexPattern}`, 'g');
return argValue.replace(regex, '');
} else {
return argValue;
}
}
function getCwd(): string {
const cwdArgument = getArgValue({ name: 'cwd', hasValue: true });
if (!cwdArgument) {
return process.cwd();
} else {
return cwdArgument.replace(/(--cwd=)/g, '');
}
}
function getPackageInfo(): any {
const content = fs.readFileSync('./package.json').toString();
return JSON.parse(content);
}
function getLatestOrPre(): 'pre' | 'latest' {
const preArgument = getArgValue({ name: 'pre' });
return preArgument ? 'pre' : 'latest';
}
function getVersion(): string {
const version = getArgValue({ name: 'version', short: 'v', hasValue: true });
if (version === undefined) {
return getPackageInfo().version;
}
if (!/\d+\.\d+\.\d+/g.test(version)) {
console.error('Version argument needs to follow the SemVer pattern: MAJOR.MINOR.PATCH.');
process.exit(1);
}
return version;
}
function getImageName(): string {
const nameArgument = getArgValue({ name: 'name', short: 'n', hasValue: true });
return nameArgument ?? DEFAULT_IMAGE_NAME;
}
function isVersionInTar(): boolean {
const noVersionInTar = getArgValue({ name: 'no-version-in-tar-name' });
return !noVersionInTar;
}
function isBundleStepActive(): boolean {
const isSkipBundleArgument = getArgValue({ name: 'bundle' });
return !!isSkipBundleArgument;
}
function getForceRemoveContainersOptions(): string {
const forceRmArg = getArgValue({ name: 'force-rm' });
return !!forceRmArg ? '--force-rm' : '';
}
function getBuildCommand(): string {
const version = getVersion();
const preOrLatest = getLatestOrPre();
const imageName = getImageName();
const forceRemoveContainers = getForceRemoveContainersOptions();
if (preOrLatest === 'latest') {
return `docker build ${forceRemoveContainers} -t=${imageName}:latest -t=${imageName}:${version} .`;
} else {
return `docker build ${forceRemoveContainers} -t=${imageName}:${version}-pre .`;
}
}
function getTarName(): string {
const version = getVersion();
const preOrLatest = getLatestOrPre();
const isVersionInTarName = isVersionInTar();
let tarName = `Tutor-Management-System`;
if (isVersionInTarName) {
tarName += `_v${version}`;
}
if (preOrLatest === 'pre') {
tarName += `-pre`;
}
return `${tarName}.tar`;
}
function getBundleCommand(): string {
const version = getVersion();
const preOrLatest = getLatestOrPre();
const imageName = getImageName();
if (preOrLatest === 'latest') {
return `docker save -o ${getTarName()} ${imageName}:latest ${imageName}:${version}`;
} else {
return `docker save -o ${getTarName()} ${imageName}:${version}-pre`;
}
}
const options: ExecSyncOptions = {
stdio: 'inherit',
cwd: getCwd(),
};
const buildCommand = getBuildCommand();
const bundleCommand = getBundleCommand();
console.log(`Running: "${buildCommand}"`);
childProcess.execSync(buildCommand, options);
if (isBundleStepActive()) {
console.log(`Running: "${bundleCommand}"`);
childProcess.execSync(bundleCommand, options);
}