-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
147 lines (123 loc) · 5.25 KB
/
build.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
import { $ } from 'bun'
// constants
const pnpmFile = './configuration/pnpm.json';
const tagsFile = './configuration/tags.json';
const [, , distribution] = Bun.argv;
// utilities
const fetchJson = async <T>(url: string) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
}
const json = await response.json();
return json as T
};
const getPnpmVersion = async () => {
const json = await fetchJson<Record<string, string>>('https://raw.githubusercontent.com/panascais-docker/node/master/configuration/pnpm.json')
const version = json[distribution] as string;
if (typeof version !== 'string' || !version?.length) {
throw new Error(`Invalid pnpm version found: '${version}' in '${JSON.stringify(json)}\n'`);
}
return version;
}
const getImageTag = async () => {
const json = await fetchJson<Record<string, string>>('https://raw.githubusercontent.com/panascais-docker/node/master/configuration/versions.json')
const tag = json[distribution] as string;
if (typeof tag !== 'string' || !tag?.length) {
throw new Error(`Invalid node tag found: '${tag}' in '${JSON.stringify(json)}\n'`);
}
return tag;
}
const getDockerfilePath = () => {
return `./${distribution}/Dockerfile`
}
const getDockerfile = async () => {
const dockerfile = await Bun.file(getDockerfilePath()).text();
return dockerfile;
}
const setDockerfile = async (dockerfile: string) => {
return Bun.write(getDockerfilePath(), dockerfile);
}
// business logic
const [pnpm, tag, dockerfile] = await Promise.all([getPnpmVersion(), getImageTag(), getDockerfile()]);
const lines = new Array<string>()
if (true as boolean) {
// process.exit(0)
}
for (const line of dockerfile.split('\n')) {
if (line.startsWith("FROM")) {
lines.push(`FROM ghcr.io/panascais-docker/node/node:${tag}`)
continue;
}
if (line.startsWith(' pnpm')) {
lines.push(` pnpm@${pnpm} \\`);
continue;
}
lines.push(line);
}
await setDockerfile(lines.join('\n'));
const [major, min, pat] = tag.split('.')
const minor = `${major}.${min}`
const patch = `${minor}.${pat}`
if (Bun.env.GITHUB_ACTIONS === 'true') {
const {
CONTAINER_REGISTRY_TOKEN,
CONTAINER_REGISTRY_USERNAME,
DOCKER_REGISTRY_TOKEN,
DOCKER_REGISTRY_USERNAME,
QUAY_REGISTRY_TOKEN,
QUAY_REGISTRY_USERNAME,
} = Bun.env;
// hack to pull first without authentication to avoid even more rate limits
await $`docker buildx create --use && docker buildx build \
--build-arg BUILD_DATE=${await $`date -u +"%Y-%m-%dT%H:%M:%SZ"`.text()} \
--build-arg NODE_VERSION=${patch} \
--build-arg VCS_REF=${await $`git rev-parse --short HEAD`.text()} \
--platform linux/amd64,linux/arm64 \
--progress=plain \
./${distribution}`;
await $`echo ${CONTAINER_REGISTRY_TOKEN} | docker login ghcr.io -u ${CONTAINER_REGISTRY_USERNAME} --password-stdin`;
await $`echo ${DOCKER_REGISTRY_TOKEN} | docker login -u ${DOCKER_REGISTRY_USERNAME} --password-stdin`;
await $`echo ${QUAY_REGISTRY_TOKEN} | docker login quay.io -u ${QUAY_REGISTRY_USERNAME} --password-stdin`;
await $`docker buildx build \
--build-arg BUILD_DATE=${await $`date -u +"%Y-%m-%dT%H:%M:%SZ"`.text()} \
--build-arg NODE_VERSION=${patch} \
--build-arg VCS_REF=${await $`git rev-parse --short HEAD`.text()} \
--push \
--platform linux/amd64,linux/arm64 \
--progress=plain \
-t ghcr.io/panascais-docker/node/ci-node:${distribution} \
-t ghcr.io/panascais-docker/node/ci-node:${major} \
-t ghcr.io/panascais-docker/node/ci-node:${minor} \
-t ghcr.io/panascais-docker/node/ci-node:${patch} \
-t panascais/ci-node:${distribution} \
-t panascais/ci-node:${major} \
-t panascais/ci-node:${minor} \
-t panascais/ci-node:${patch} \
-t quay.io/panascais/ci-node:${distribution} \
-t quay.io/panascais/ci-node:${major} \
-t quay.io/panascais/ci-node:${minor} \
-t quay.io/panascais/ci-node:${patch} \
./${distribution}`;
} else {
await $`docker buildx create --name orb --use &> /dev/null && docker buildx inspect --bootstrap &> /dev/null && docker buildx install &> /dev/null || true && docker buildx build \
--build-arg BUILD_DATE=${await $`date -u +"%Y-%m-%dT%H:%M:%SZ"`.text()} \
--build-arg NODE_VERSION=${patch} \
--build-arg VCS_REF=${await $`git rev-parse --short HEAD`.text()} \
--load \
--platform linux/arm64 \
--progress=plain \
-t ghcr.io/panascais-docker/node/ci-node:${distribution} \
-t ghcr.io/panascais-docker/node/ci-node:${major} \
-t ghcr.io/panascais-docker/node/ci-node:${minor} \
-t ghcr.io/panascais-docker/node/ci-node:${patch} \
-t panascais/ci-node:${distribution} \
-t panascais/ci-node:${major} \
-t panascais/ci-node:${minor} \
-t panascais/ci-node:${patch} \
-t quay.io/panascais/ci-node:${distribution} \
-t quay.io/panascais/ci-node:${major} \
-t quay.io/panascais/ci-node:${minor} \
-t quay.io/panascais/ci-node:${patch} \
./${distribution}`;
}