-
Notifications
You must be signed in to change notification settings - Fork 66
/
build-image.sh
executable file
·124 lines (106 loc) · 2.61 KB
/
build-image.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
#!/bin/bash
SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))
OUT=$SRC/out
TARGETS=()
TAGS=()
VERSION=
PUSH=0
IMAGE=docker.io/chromedp/headless-shell
DOCKER_USER=kenshaw
DOCKER_PASSFILE=$HOME/.config/headless-shell/token
OPTIND=1
while getopts "o:t:g:v:pi:" opt; do
case "$opt" in
o) OUT=$OPTARG ;;
t) TARGETS+=($OPTARG) ;;
g) TAGS+=($OPTARG) ;;
v) VERSION=$OPTARG ;;
p) PUSH=1 ;;
i) IMAGE=$OPTARG ;;
esac
done
set -e
# check out dir
if [ ! -d $OUT ]; then
echo "$OUT does not exist!"
exit 1
fi
# determine version
if [ -z "$VERSION" ]; then
VERSION=$(ls $OUT/*.bz2|sort -r -V|head -1|sed -e 's/.*headless-shell-\([0-9\.]\+\).*/\1/')
fi
# determine targets
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=($(ls $OUT/*-${VERSION}-*.bz2|sed -e 's/.*headless-shell-[0-9\.]\+-\([a-z0-9]\+\).*/\1/'|xargs))
fi
# join_by ',' ${A[@]} ${B[@]}
join_by() {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
echo "VERSION: $VERSION [${TARGETS[@]}]"
echo "IMAGE: $IMAGE [tags: $(join_by ' ' $VERSION ${TAGS[@]})]"
IMAGES=()
for TARGET in ${TARGETS[@]}; do
NAME=localhost/$(basename $IMAGE):$VERSION-$TARGET
IMAGES+=($NAME)
if [ ! -z "$(buildah images --noheading --filter=reference=$NAME)" ]; then
echo -e "\n\nSKIPPING BUILD FOR $NAME ($(date))"
continue
fi
echo -e "\n\nBUILDING $NAME ($(date))"
ARCHIVE=$OUT/headless-shell-$VERSION-$TARGET.tar.bz2
if [ ! -f $ARCHIVE ]; then
echo "ERROR: $ARCHIVE is missing!"
exit 1
fi
(set -x;
rm -rf $OUT/$VERSION-$TARGET
mkdir -p $OUT/$VERSION-$TARGET
tar -C $OUT/$VERSION-$TARGET -jxf $ARCHIVE
buildah build \
--platform linux/$TARGET \
--build-arg VERSION="$VERSION-$TARGET" \
--tag $NAME \
$SRC
)
done
(set -x;
buildah login docker.io \
--username $DOCKER_USER \
--password-stdin < $DOCKER_PASSFILE
)
REPO=$(sed -e 's%^docker\.io/%%' <<< "$IMAGE")
for TAG in $VERSION ${TAGS[@]}; do
NAME=localhost/$(basename $IMAGE):$TAG
# create manifest
echo -e "\n\nCONFIGURING MANIFEST $NAME ($(date))"
if `buildah manifest exists $NAME`; then
for HASH in $(buildah manifest inspect $NAME|jq -r '.manifests[]|.digest'); do
(set -x;
buildah manifest remove $NAME $HASH
)
done
else
(set -x;
buildah manifest create $NAME
)
fi
# add images
for IMG in ${IMAGES[@]}; do
(set -x;
buildah manifest add $NAME $IMG
)
done
if [ $PUSH -eq 1 ]; then
echo -e "\n\nPUSHING MANIFEST $NAME ($(date))"
(set -x;
buildah manifest push \
--all \
$NAME \
docker://$REPO:$TAG
)
fi
done