-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-experimental.sh
executable file
·385 lines (324 loc) · 10.5 KB
/
publish-experimental.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash -eu
# Publish any versions of the docker image not yet pushed to jenkins/jenkins
# Arguments:
# -n dry run, do not build or publish images
# -d debug
set -eou pipefail
ARCHS=(arm arm64 s390x ppc64le amd64)
QEMUARCHS=(arm aarch64 s390x ppc64le x86_64)
QEMUVER="v2.12.0-1"
REGISTRY="jenkins4eval"
IMAGE="jenkins-multiarch-qemu"
BASEIMAGE=
get-manifest-tool() {
if [[ ! -f manifest-tool ]]; then
local version
version=$(curl -s https://api.github.com/repos/estesp/manifest-tool/tags | jq -r '.[0].name')
echo "Downloading manifest-tool"
if ! curl -OLs "https://github.com/estesp/manifest-tool/releases/download/$version/manifest-tool-linux-amd64"; then
echo "Error downloading manifest-tool"
exit
fi
mv manifest-tool-linux-amd64 manifest-tool
chmod +x manifest-tool
fi
}
get-qemu-handlers() {
if [[ ! $(find ./multiarch -name "*qemu-*") ]]; then
pushd multiarch
echo "Downloading Qemu handlers"
for target_arch in ${QEMUARCHS[*]}; do
if ! curl -OLs "https://github.com/multiarch/qemu-user-static/releases/download/$QEMUVER/x86_64_qemu-${target_arch}-static.tar.gz"; then
echo "Error downloading Qemu handler"
exit
fi
tar -xvf x86_64_qemu-"${target_arch}"-static.tar.gz
done
rm -f x86_64_qemu-*
popd
fi
}
set-base-image() {
local variant=$1
local arch=$2
local dockerfile
if [[ ! -z "$variant" ]]; then
dockerfile="./multiarch/Dockerfile${variant}-${arch}"
else
dockerfile="./multiarch/Dockerfile-${arch}"
fi
if [[ "$variant" =~ alpine ]]; then
/bin/cp -f multiarch/Dockerfile.alpine "$dockerfile"
elif [[ "$variant" =~ slim ]]; then
/bin/cp -f multiarch/Dockerfile.slim "$dockerfile"
else
/bin/cp -f multiarch/Dockerfile.debian "$dockerfile"
fi
# Parse architectures and variants
if [[ $arch == amd64 ]]; then
BASEIMAGE="openjdk:8-jdk"
elif [[ $arch == arm ]]; then
BASEIMAGE="arm32v7/openjdk:8-jdk"
elif [[ $arch == arm64 ]]; then
BASEIMAGE="arm64v8/openjdk:8-jdk"
elif [[ $arch == s390x ]]; then
BASEIMAGE="s390x/openjdk:8-jdk"
elif [[ $arch == ppc64le ]]; then
BASEIMAGE="ppc64le/openjdk:8-jdk"
fi
# The Alpine image only supports arm32v6 but should work fine on arm32v7
# hardware - https://github.com/moby/moby/issues/34875
if [[ $variant =~ alpine && $arch == arm ]]; then
BASEIMAGE="arm32v6/openjdk:8-jdk-alpine"
elif [[ $variant =~ alpine ]]; then
BASEIMAGE="$BASEIMAGE-alpine"
elif [[ $variant =~ slim ]]; then
BASEIMAGE="$BASEIMAGE-slim"
fi
# Make the Dockerfile after we set the base image
sed -i "s|BASEIMAGE|${BASEIMAGE}|g" "$dockerfile"
if [[ "${arch}" == "amd64" ]]; then
sed -i "/CROSS_BUILD_/d" "$dockerfile"
else
if [[ "${arch}" == "arm64" ]]; then
sed -i "s|ARCH|aarch64|g" "$dockerfile"
else
sed -i "s|ARCH|${arch}|g" "$dockerfile"
fi
sed -i "s/CROSS_BUILD_//g" "$dockerfile"
fi
}
sort-versions() {
if [ "$(uname)" == 'Darwin' ]; then
gsort --version-sort
else
sort --version-sort
fi
}
# Try tagging with and without -f to support all versions of docker
docker-tag() {
local from="$REGISTRY/$IMAGE:$1"
local to="$2/$IMAGE:$3"
local out
docker pull "$from"
if out=$(docker tag -f "$from" "$to" 2>&1); then
echo "$out"
else
docker tag "$from" "$to"
fi
}
login-token() {
# could use jq .token
curl -q -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:jmreicha/jenkins:pull" | grep -o '"token":"[^"]*"' | cut -d':' -f 2 | xargs echo
}
is-published() {
local tag=$1
local opts=""
if [ "$debug" = true ]; then
opts="-v"
fi
local http_code;
http_code=$(curl $opts -q -fsL -o /dev/null -w "%{http_code}" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jmreicha/jenkins/manifests/$tag")
if [ "$http_code" -eq "404" ]; then
false
elif [ "$http_code" -eq "200" ]; then
true
else
echo "Received unexpected http code from Docker hub: $http_code"
exit 1
fi
}
get-manifest() {
local tag=$1
local opts=""
if [ "$debug" = true ]; then
opts="-v"
fi
curl $opts -q -fsSL -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Bearer $TOKEN" "https://index.docker.io/v2/jmreicha/jenkins/manifests/$tag"
}
get-digest() {
local manifest
manifest=$(get-manifest "$1")
#get-manifest "$1" | jq .config.digest
if [ "$debug" = true ]; then
>&2 echo "DEBUG: Manifest for $1: $manifest"
fi
echo "$manifest" | grep -A 10 -o '"config".*' | grep digest | head -1 | cut -d':' -f 2,3 | xargs echo
}
get-latest-versions() {
curl -q -fsSL https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-war/maven-metadata.xml | grep '<version>.*</version>' | grep -E -o '[0-9]+(\.[0-9]+)+' | sort-versions | uniq | tail -n 1
}
# Make a list of platforms for manifest-tool to publish
parse-manifest-platforms() {
local platforms=()
for arch in ${ARCHS[*]}; do
platforms+=("linux/$arch")
done
IFS=,;printf "%s" "${platforms[*]}"
}
publish() {
local version=$1
local variant=$2
local tag="${version}${variant}"
local sha
build_opts=(--no-cache --pull)
if [ "$dry_run" = true ]; then
build_opts=()
fi
sha=$(curl -q -fsSL "https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-war/${version}/jenkins-war-${version}.war.sha256" )
for arch in ${ARCHS[*]}; do
set-base-image "$variant" "$arch"
docker build --file "multiarch/Dockerfile$variant-$arch" \
--build-arg "JENKINS_VERSION=$version" \
--build-arg "JENKINS_SHA=$sha" \
--tag "$REGISTRY/$IMAGE:${tag}-${arch}" \
"${build_opts[@]+"${build_opts[@]}"}" .
# " line to fix syntax highlightning
if [ ! "$dry_run" = true ]; then
docker push "$REGISTRY/$IMAGE:${tag}-${arch}"
fi
done
}
tag-and-push() {
local source=$1
local target=$2
local arch=$3
local digest_source
local digest_target
if [ "$debug" = true ]; then
>&2 echo "DEBUG: Getting digest for ${source}-${arch}"
fi
# if tag doesn't exist yet, ie. dry run
if ! digest_source=$(get-digest "${source}-${arch}"); then
echo "Unable to get source digest for ${source}-${arch} ${digest_source}"
digest_source=""
fi
if [ "$debug" = true ]; then
>&2 echo "DEBUG: Getting digest for ${target}-${arch}"
fi
if ! digest_target=$(get-digest "${target}-${arch}"); then
echo "Unable to get target digest for ${target}-${arch} ${digest_target}"
digest_target=""
fi
if [ "$digest_source" == "$digest_target" ] && [ -n "${digest_target}" ]; then
echo "Images ${source}-${arch} [$digest_source] and ${target}-${arch} [$digest_target] are already the same, not updating tags"
else
echo "Creating tag ${target}-${arch} pointing to ${source}-${arch}"
#docker-tag "${source}-${arch}" "jenkins" "${target}-${arch}"
docker-tag "${source}-${arch}" "$REGISTRY" "${target}-${arch}"
if [ ! "$dry_run" = true ]; then
echo "Pushing $REGISTRY/$IMAGE:${target}-${arch}"
docker push "$REGISTRY/$IMAGE:${target}-${arch}"
else
echo "Would push $REGISTRY/$IMAGE:${target}-${arch}"
fi
fi
}
publish-variant() {
local version=$1
local variant=$2
for arch in ${ARCHS[*]}; do
if [[ "$variant" =~ slim ]]; then
tag-and-push "${version}${variant}" "slim" "${arch}"
elif [[ "$variant" =~ alpine ]]; then
tag-and-push "${version}${variant}" "alpine" "${arch}"
fi
done
if [[ "$variant" =~ slim ]]; then
push-manifest "slim" ""
elif [[ "$variant" =~ alpine ]]; then
push-manifest "alpine" ""
fi
}
publish-latest() {
local version=$1
local variant=$2
for arch in ${ARCHS[*]}; do
# push latest (for master) or the name of the branch (for other branches)
if [ -z "$variant" ]; then
tag-and-push "${version}${variant}" "latest" "${arch}"
fi
done
# Only push latest when there is no variant
if [[ -z "$variant" ]]; then
push-manifest "latest" ""
fi
}
publish-lts() {
local version=$1
local variant=$2
for arch in ${ARCHS[*]}; do
tag-and-push "${version}${variant}" "lts${variant}" "${arch}"
done
push-manifest "lts" "${variant}"
}
push-manifest() {
local version=$1
local variant=$2
./manifest-tool push from-args \
--platforms "$(parse-manifest-platforms)" \
--template "$REGISTRY/$IMAGE:${version}${variant}-ARCH" \
--target "$REGISTRY/$IMAGE:${version}${variant}"
}
cleanup() {
echo "Cleaning up"
rm -f manifest-tool
rm -f ./multiarch/qemu-*
rm -rf ./multiarch/Dockerfile-*
}
# Process arguments
dry_run=false
debug=false
variant=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-n)
dry_run=true
;;
-d)
debug=true
;;
-v|--variant)
variant="-"$2
shift
;;
*)
echo "Unknown option: $key"
return 1
;;
esac
shift
done
if [ "$dry_run" = true ]; then
echo "Dry run, will not publish images"
fi
if [ "$debug" = true ]; then
set -x
fi
get-manifest-tool
get-qemu-handlers
# Register binfmt_misc to run cross platform builds against non x86 architectures
docker run --rm --privileged multiarch/qemu-user-static:register --reset
TOKEN=$(login-token)
lts_version=""
version=""
for version in $(get-latest-versions); do
if is-published "$version$variant"; then
echo "Tag is already published: $version$variant"
else
echo "Publishing version: $version$variant"
publish "$version" "$variant"
fi
# Update lts tag
if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
lts_version="${version}"
fi
done
push-manifest "${version}" "${variant}"
publish-variant "${version}" "${variant}"
publish-latest "${version}" "${variant}"
if [ -n "${lts_version}" ]; then
publish-lts "${lts_version}" "${variant}"
fi
cleanup