-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildtagpush
executable file
Β·183 lines (148 loc) Β· 4.17 KB
/
buildtagpush
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
#!/usr/bin/env bash
set -euo pipefail
_output() {
echo ""
echo "π (${SECONDS}s) $@"
}
_output "START: $(date)"
case "${1:-}" in
trypull)
images="${@:2}"
_output "try pull"
for image in $images; do
(
docker pull "$image" || true
) 2>&1 | sed -le "s#^#π (${SECONDS}s) trypull $image -- #;" &
done
wait $(jobs -p)
_output "OK -- trypull"
exit 0
;;
esac
export BUILDKIT_PROGRESS=plain
export DOCKER_BUILDKIT=1
tag=${TAG:-latest}
export CACHE_TAG=${CACHE_TAG:-$tag}
commit_timestamp_utc=$(TZ=UTC0 git show --quiet --date='format-local:%Y%m%d%H%M%S' --format="%cd")
export TAG="${commit_timestamp_utc}-${tag}"
export WORKDIR=${WORKDIR:-/workspace}
export DOCKERFILE=${DOCKERFILE:-Dockerfile}
export BUILD_OPTS=${BUILD_OPTS:-}
export TRIES=${TRIES:-6}
cd "$WORKDIR"
_output "pwd: $(pwd)"
_output "nproc: $(nproc)"
_output "docker version:"
_output "TAG: ${TAG}"
_output "CACHE_TAG: ${CACHE_TAG}"
_output "WORKDIR: ${WORKDIR}"
_output "DOCKERFILE: ${DOCKERFILE}"
_output "BUILD_OPTS: ${BUILD_OPTS}"
_output "TRIES: ${TRIES}"
printf "$(docker version)\n"
_output "export:"
export
if >/dev/null DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect "$REGISTRY/$IMAGE:$TAG"; then
_output "Image $REGISTRY/$IMAGE:$TAG already exists, stopping here."
_output "OK -- NOP"
exit 0
fi
_output "Dockerfile: ${DOCKERFILE}"
cat "$DOCKERFILE"
_output "PULL images"
# TODO: how does this work with date tag ?
if [ "$CACHE_TAG" != "$TAG" ]; then
(
pulled=no
for ((try=1; try<=$TRIES; try++)); do
if docker pull "$REGISTRY/$IMAGE:$CACHE_TAG"; then
pulled=yes
break
fi
_output "pull failed, retry"
sleep 1
done
if [[ "$pulled" == "no" ]]; then
_output "pull failed, this is okay as it is cache"
fi
) 2>&1 | sed -le "s#^#π (${SECONDS}s) pull cache :$CACHE_TAG -- #;" &
fi
wait $(jobs -p)
_output "BUILD $REGISTRY/$IMAGE:$TAG with cache from $REGISTRY/$IMAGE:$CACHE_TAG"
(
for ((try=1; try<=$TRIES; try++)); do
# NOTE: $BUILD_OPTS needs to be without quotes to expand properly
if docker build -f "$DOCKERFILE" --cache-from="$REGISTRY/$IMAGE:$CACHE_TAG" --build-arg BUILDKIT_INLINE_CACHE=1 -t "$REGISTRY/$IMAGE:$TAG" $BUILD_OPTS .; then
break
else
if [ "$try" = "$TRIES" ]; then
echo "last attempt failed, exit 1"
exit 1
else
echo "$try attempt failed, sleep $try"
sleep "$try"
fi
echo "trying to pull deps"
# https://github.com/moby/moby/issues/40262#issuecomment-656036657)
from_images=""
copy_froms=""
set +e
# remofe --platform=... etc
from_images=$(cat "$DOCKERFILE" | grep FROM | sed 's/\s--.*\s/ /' | cut -d' ' -f2)
copy_froms=$(cat "$DOCKERFILE" | grep "COPY --from=" | cut -d'=' -f2 | cut -d' ' -f1)
set -e
for image in $from_images $copy_froms; do
case $image in
scratch|cache)
continue
;;
*:${CACHE_TAG})
continue
;;
esac
(
docker pull "$image" || true
) 2>&1 | sed -le "s#^#π (${SECONDS}s) pull dep $image -- #;" &
done
wait $(jobs -p)
echo "deps pulled, trying one more time"
fi
done
) 2>&1 | sed -le "s#^#π (${SECONDS}s) build -- #;"
(
exec docker tag "$REGISTRY/$IMAGE:$TAG" "$REGISTRY/$IMAGE:$CACHE_TAG"
) 2>&1 | sed -le "s#^#π (${SECONDS}s) tag -- #;"
(
pushed=no
for ((try=1; try<=$TRIES; try++)); do
if docker push "$REGISTRY/$IMAGE:$TAG"; then
pushed=yes
break
fi
_output "push failed, retry"
sleep 1
done
if [[ "$pushed" == "no" ]]; then
_output "push failed."
exit 1
fi
) 2>&1 | sed -le "s#^#π (${SECONDS}s) push tag :$TAG -- #;" &
if [ "$CACHE_TAG" != "$TAG" ]; then
(
pushed=no
for ((try=1; try<=$TRIES; try++)); do
if docker push "$REGISTRY/$IMAGE:$CACHE_TAG"; then
pushed=yes
break
fi
_output "push failed, retry"
sleep 1
done
if [[ "$pushed" == "no" ]]; then
_output "push failed."
exit 1
fi
) 2>&1 | sed -le "s#^#π (${SECONDS}s) push cache :$CACHE_TAG -- #;"
fi
wait
_output "OK -- all"