-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.js
70 lines (48 loc) · 1.98 KB
/
push.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
import { $ } from 'bun';
const img = 'nozich/bunode'
async function main() {
await $`docker pull ${img}:latest`
const inspect = await $`docker buildx inspect | grep 'Platforms:' | sed 's/Platforms: //'`.text();
const platforms = inspect.split(',').map(e => e.trim()).map((p) => ({
platform: p,
os: p.split('/')[0],
arch: p.split('/')[1],
tag: p.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase()
}));
console.log(`platforms`, platforms.map((p) => p.tag).join(','));
const built = (await Promise.all(platforms.map(async ({ platform, tag }) => {
const build = await $`docker buildx build --platform=${platform} -t ${img}:${tag} --push .`.text()
.catch((e) => e);
console.log(`[BUILD] [END] [${platform}] [${tag}] {exitCode: ${build.exitCode}}`);
return (build.exitCode === 0) && tag
}))).filter(Boolean);
console.log(`built`, built);
if (built.length === 0) {
console.error('No images were built');
return;
}
const currents = await fetch(`https://registry.hub.docker.com/v2/repositories/${img}/tags/`)
.then((res) => res.json())
.then((data) => data.results
.find((r) => r.name === 'latest')
.images.map((i) => ({
...i,
arch: i.architecture,
tag: i.variant ? `${i.os}-${i.architecture}-${i.variant}` : `${i.os}-${i.architecture}`
})));
const annotates = currents || []
const news = platforms.filter((p) => built.includes(p.tag))
news.forEach((p) => {
if (annotates.find((a) => a.tag === p.tag)) return;
annotates.push(p);
});
console.log(`annotates`, annotates.map(({ tag, os, arch }) => ({ tag, os, arch })));
const tags = annotates.map(({ tag }) => `${img}:${tag}`)
tags.unshift(`${img}:latest`)
await $`docker manifest create --amend ${tags}`;
await Promise.all(annotates.map(async ({ os, arch, tag }) => {
return $`docker manifest annotate ${img}:latest ${img}:${tag} --os ${os} --arch ${arch}`;
}));
await $`docker manifest push --purge ${img}:latest`
}
main();