-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
252 lines (212 loc) · 7.95 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const YAML = require('yaml')
const fs = require('fs');
const Twig = require("twig");
const util = require('util');
const child_process = require('child_process');
const fg = require('fast-glob');
const os = require('os');
const renderFile = util.promisify(Twig.renderFile);
let config = YAML.parse(fs.readFileSync(__dirname + '/build.yaml', 'utf8'));
const command = process.argv[2];
const selectedTag = process.argv[3] || null;
const imageSuffix = process.argv[4] || '';
const defaultVars = { _arch: os.arch() };
const run = async() => {
config = await prepareConfig(config);
if (command === 'buildTag') {
for (let image of config.images) {
if (selectedTag && !image.buildTags.includes(selectedTag)) {
continue;
}
for (let tagName of Object.keys(image.tags)) {
let tag = image.tags[tagName];
let cleanupList = [];
// Render templates
let variables = {...config.variables, ... image.variables, ... tag, ... defaultVars};
variables._image = image;
variables._tag = tag;
for (let fileName of Object.keys(image.templates)) {
let saveFile = image.templates[fileName];
let content = await renderFile(fileName, variables);
fs.writeFileSync(saveFile, content);
cleanupList.push(saveFile);
}
if (typeof image.image === 'string') {
image.image = [image.image];
}
// Build that image
for (let imageName of image.image) {
await exec('docker', ['build', '-t', `${imageName}:${tagName}`, `-f`, image.dockerFile, image.context], `Building ${imageName}:${tagName}`);
}
// Cleanup rendered files
for (let file of cleanupList) {
fs.unlinkSync(file);
}
}
}
}
if (command === 'buildAndPushCI') {
for (let image of config.images) {
for (let tagName of Object.keys(image.tags)) {
let tag = image.tags[tagName];
const fqnImageName = `${image.image[0]}:${tagName}`;
if (selectedTag !== fqnImageName) {
continue;
}
let cleanupList = [];
// Render templates
let variables = {...config.variables, ... image.variables, ... tag, ... defaultVars};
variables._image = image;
variables._tag = tag;
for (let fileName of Object.keys(image.templates)) {
let saveFile = image.templates[fileName];
let content = await renderFile(fileName, variables);
fs.writeFileSync(saveFile, content);
cleanupList.push(saveFile);
}
// Build that image
for (let imageName of image.image) {
try {
await exec('docker', ['build', '-t', `${imageName}:${tagName}${imageSuffix}`, '-f', image.dockerFile, image.context]);
await exec('docker', ['push', `${imageName}:${tagName}${imageSuffix}`]);
} catch (e) {
process.exit(1);
}
}
// Cleanup rendered files
for (let file of cleanupList) {
fs.unlinkSync(file);
}
}
}
}
if (command === 'generateJobs') {
const ghConfig = {
amd64: {
'fail-fast': false,
matrix: {
include: []
}
},
arm64: {
'fail-fast': false,
matrix: {
include: []
}
},
merge: {
'fail-fast': false,
matrix: {
include: []
}
}
};
const buildName = (vars, imageName) => {
if (vars === null) {
return imageName;
}
if (vars.phpVersion !== undefined) {
return `PHP: ${vars.phpVersion}`;
}
if (vars.mysqlVersion !== undefined) {
return `MySQL: ${vars.mysqlVersion}`;
}
return buildName(null, imageName);
};
let mergeCommands = '';
for (let image of config.images) {
for (let tagName of Object.keys(image.tags)) {
const fqnImageName = image.image[0] + ':' + tagName;
// amd64 only build
if (image.platforms.length === 1) {
ghConfig.amd64.matrix.include.push({
name: `amd64/${fqnImageName}`,
os: 'ubuntu-latest',
runs: {
build: `npm install; node build.js buildAndPushCI ${fqnImageName}`
}
})
} else {
ghConfig.amd64.matrix.include.push({
name: `amd64/${fqnImageName}`,
os: 'ubuntu-latest',
runs: {
build: `npm install; node build.js buildAndPushCI ${fqnImageName} -amd64`
}
});
ghConfig.arm64.matrix.include.push({
name: `arm64/${fqnImageName}`,
os: 'ARM64',
runs: {
build: `npm install; node build.js buildAndPushCI ${fqnImageName} -arm64`
}
})
mergeCommands = `${mergeCommands}docker manifest create ${fqnImageName} --amend ${fqnImageName}-amd64 --amend ${fqnImageName}-arm64;` + "\n"
mergeCommands = `${mergeCommands}docker manifest push ${fqnImageName};` + "\n"
}
}
}
ghConfig.merge.matrix.include.push({
name: 'Create Manifest',
runs: {
merge: mergeCommands
}
});
console.log(JSON.stringify(ghConfig));
}
};
const exec = (command, args, prefix) => {
return new Promise((resolve, reject) => {
const child = child_process.spawn(command, args);
child.stdout.on('data', (chunk) => {
process.stdout.write(`${prefix}: ${chunk}`);
});
child.stderr.on('data', (chunk) => {
console.log(`Stderr: ${chunk}`);
});
child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(child);
}
});
});
};
const prepareConfig = async (config) => {
config = addDefaults(config);
const entries = await fg(config.includes, { dot: false });
for (let otherConfig of entries) {
otherConfig = addDefaults(YAML.parse(fs.readFileSync(__dirname + '/' + otherConfig, 'utf8')));
config.images = config.images.concat(otherConfig.images);
config.variables = {... config.variables, ...otherConfig.variables};
}
return config;
};
const addDefaults = (config) => {
if (config.includes === undefined) {
config.includes = [];
}
if (config.variables === undefined) {
config.variables = [];
}
if (config.images === undefined) {
config.images = [];
}
for (let image of config.images) {
if (image.platforms === undefined) {
image.platforms = ['linux/amd64'];
}
if (image.buildTags === undefined) {
image.buildTags = [];
}
if (image.variables === undefined) {
image.variables = [];
}
if (image.templates === undefined) {
image.templates = [];
}
}
return config;
};
run();