-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·305 lines (286 loc) · 10.3 KB
/
build.sh
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
versions=$(
echo 'bookworm' && \
echo 'bullseye' && \
echo 'buster' && \
echo 'stretch' && \
echo 'jessie' && \
echo 'wheezy'
)
detectVersion () {
# detection the major version of the image
version=""
if [[ "${version}" == "" ]] && [[ -f hashes/$1 ]] && [[ "$2" == "" ]]; then
version=$(cat hashes/$1 | grep 'version' | sed -e 's/^version://')
fi
if [[ "${version}" == "" ]]; then
for possibleVersion in $versions; do
if [[ $1 == *"${possibleVersion}"* ]]; then
version="${possibleVersion}"
fi
done
fi
if [[ "${version}" == "" ]]; then
docker image pull node:$1 > /dev/null
version=$(
docker run --rm node:$1 cat /etc/os-release | \
grep 'VERSION=' | \
grep -oE '\(.*\)' | \
grep -oE '\w+' || \
echo ''
)
fi
if [[ "${version}" == "" ]]; then
docker image pull node:$1 > /dev/null
version=$(
docker run --rm node:$1 cat /etc/os-release | \
grep 'PRETTY_NAME=' | \
grep -oE '\w+/\w+"' | \
grep -oE '\w+/' | \
grep -oE '\w+' || \
echo ''
)
fi
if [[ "${version}" == "" ]]; then
version=$(cat hashes/$1 | grep 'version' | sed -e 's/^version://')
fi
echo $version
}
detectDockerfile () {
# detecting suitable docker file
dockerfile="docker/Dockerfile"
if [[ -f docker/$1/Dockerfile ]]; then
dockerfile="docker/$1/Dockerfile"
fi
echo $dockerfile;
}
if [[ ! -d ./buildx-data ]]; then
mkdir ./buildx-data
fi
if [[ ! -d ./buildx-data/index ]]; then
mkdir ./buildx-data/index
fi
if [[ -f .url ]]; then
URL=$(cat .url)
fi
tagsInclude=""
if [[ $URL == "" ]]; then
URL=https://registry.hub.docker.com/v2/repositories/library/node/tags
tagsInclude="${versions}"
fi
while [[ $URL != "" ]]; do
echo $URL > .url
exitCode=1
while [[ $exitCode != 0 ]]; do
content=$(curl -sL $URL)
exitCode=$?
echo "$exitCode - $URL"
done
URL=$(
echo $content | \
grep -oE '"next":"https://registry.hub.docker.com/v2/[^"]+"' | \
sed -e 's/^"next":"//' | \
sed -e 's/"$//' | \
sed -e 's/\\u0026/\&/'
)
tags=$(
echo "${tagsInclude}" && \
echo $content | \
grep -oE '"name":"[^"]+"' | \
sed -e 's/^"name":"//' | \
sed -e 's/"$//' | \
grep -v 'alpine' | \
grep -v 'onbuild'
)
tags=$(echo "${tags}" | sed -E '/^$/d')
tagsInclude=""
for tag in $tags; do
exitCode=1
while [[ $exitCode != 0 ]]; do
content=$(curl -sL https://registry.hub.docker.com/v2/repositories/library/node/tags/$tag)
exitCode=$?
echo "$exitCode - https://registry.hub.docker.com/v2/repositories/library/node/tags/$tag"
done
version=$(detectVersion $tag)
dockerfile=$(detectDockerfile $version)
if [[ "${version}" == "" ]]; then
echo "Cannot detect version of ${tag}"
exit 1
fi
md5=""
if [[ "$(which md5)" != "" ]]; then
md5=$(md5 -q $dockerfile)
fi
if [[ "$(which md5sum)" != "" ]]; then
md5=$(md5sum $dockerfile | grep -oE '^[^ ]+')
fi
if [[ "${md5}" == "" ]]; then
echo "Cannot calculate md5 sum for the template"
exit 1
fi
digestCurrent=$(
echo $content | \
grep -oE '"digest":"[^"]+"' | \
sed -e 's/^"digest":"//' | \
sed -e 's/"$//' | \
sort | \
uniq && \
echo dockerfile:${md5} && \
echo version:${version}
)
platforms=$(
echo $content | \
grep -oE '"architecture":"[^"]+"' | \
sed -e 's/^"architecture":"//' | \
sed -e 's/"$//' | \
sed '/unknown/d' | \
sort | \
uniq
)
platforms=$(
echo "$platforms" | \
sed -e 's/^/linux\//' | \
tr '\n' ',' | \
sed -e 's/,$//'
)
if [[ "${platforms}" == "linux/" ]]; then
platforms="linux/amd64"
fi
if [[ "${platforms}" == "" ]]; then
platforms="linux/amd64"
fi
tagType=$(
echo $content | jq -r .content_type
)
digestOld=$(cat hashes/$tag 2> /dev/null)
digestBuildX=""
if [[ "${digestOld}" != "" ]]; then
digestBuildX=$(echo "${digestOld}" | grep 'buildx:' | sed -E 's/^buildx\:/--cache-from type=local,src=.\/buildx-data,digest=/g')
digestOld=$(echo "${digestOld}" | sed -E '/buildx\:/d')
fi
if [[ "${digestCurrent}" != "" ]]; then
currentBuildFile=""
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5)
fi
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the digestCurrent"
exit 1
fi
if [[ -f "./buildx-data/index/${currentBuildFile}" ]]; then
digestBuildX=$(
echo "${digestBuildX}" && \
echo "--cache-from type=local,src=./buildx-data,digest=$(cat ./buildx-data/index/${currentBuildFile})"
)
fi
fi
if [[ "${digestOld}" != "" ]]; then
currentBuildFile=""
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestOld}" | grep -oE '^sha256:.*$' | md5)
fi
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestOld}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the digestOld"
exit 1
fi
if [[ -f "./buildx-data/index/${currentBuildFile}" ]]; then
digestBuildX=$(
echo "${digestBuildX}" && \
echo "--cache-from type=local,src=./buildx-data,digest=$(cat ./buildx-data/index/${currentBuildFile})"
)
fi
fi
if [[ "${digestOld}" != "" ]] && [[ "$(echo "$digestOld" | grep version:)" == "" ]]; then
echo version:${version} >> hashes/$tag
git add hashes/$tag
git commit -m "chore($tag): version" hashes/$tag
digestOld=$(
echo "$digestOld" && \
echo version:${version}
)
fi
if [[ "$(echo "$digestCurrent" | sort)" != "$(echo "$digestOld" | sort)" ]] && [[ $digestCurrent != "" ]] || [[ -f hashes/$tag.error ]] || [[ -f "hashes/${tag}@error" ]]; then
version=$(detectVersion $tag true)
dockerfile=$(detectDockerfile $version)
echo Tag: $tag
echo Version: $version
echo Dockerfile: $dockerfile
echo Caches: $digestBuildX
echo "FROM node:${tag}" > Dockerfile && \
cat $dockerfile >> Dockerfile && \
if [[ "${tagType}" == "image" ]]; then
$(
docker buildx build \
--builder puppeteer-node \
${digestBuildX} \
--cache-to type=local,dest=./buildx-data \
--add-host archive.debian.org.lo:172.16.0.1 \
--add-host deb.debian.org.lo:172.16.0.1 \
--add-host http.debian.net:172.16.0.1 \
--add-host httpredir.debian.org.lo:172.16.0.1 \
--add-host security.debian.org.lo:172.16.0.1 \
--add-host snapshot.debian.org.lo:172.16.0.1 \
--platform $platforms \
--tag satantime/puppeteer-node:$tag --push .
)
elif [[ "${tagType}" == "unrecognized" ]] || [[ -z "${tagType}" ]]; then
docker build \
--add-host archive.debian.org.lo:172.16.0.1 \
--add-host deb.debian.org.lo:172.16.0.1 \
--add-host http.debian.net:172.16.0.1 \
--add-host httpredir.debian.org.lo:172.16.0.1 \
--add-host security.debian.org.lo:172.16.0.1 \
--add-host snapshot.debian.org.lo:172.16.0.1 \
--tag satantime/puppeteer-node:$tag .
docker push satantime/puppeteer-node:$tag
else
echo "Unknown tagType: ${tagType}"
exit 1
fi && \
digestCurrent=$(echo "${digestCurrent}" | sed -E '/version:/d' && echo "version:${version}") && \
digestBuildX=$(cat ./buildx-data/index.json | jq -r '.manifests[].digest') && \
digestCurrent=$(echo "${digestCurrent}" | sed -E '/buildx:/d' && echo "buildx:${digestBuildX}") && \
currentBuildFile="" && \
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5)
fi && \
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi && \
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the template"
exit 1
fi && \
echo "${digestBuildX}" > "./buildx-data/index/${currentBuildFile}" && \
rm Dockerfile
code="${?}"
if [[ -f "hashes/${tag}.error" ]]; then
rm "hashes/${tag}.error"
git rm -f "hashes/${tag}.error"
fi
if [[ -f "hashes/${tag}@error" ]]; then
rm "hashes/${tag}@error"
git rm -f "hashes/${tag}@error"
fi
if [[ "${code}" == "0" ]]; then
printf '%s\n' $digestCurrent > hashes/$tag
git add hashes/$tag
git commit -m "chore($tag): updated" "hashes/${tag}"
fi
if [[ "${code}" != "0" ]]; then
printf '%s\n' $digestCurrent > "hashes/${tag}@error"
git add "hashes/${tag}@error"
fi
sleep 10
fi
true;
done
true;
done
rm .url