Skip to content

Commit ca8724a

Browse files
clauderjhuijsman
authored andcommitted
fixup! Add a pre-pull-docker-image composite action
1 parent 459dbd5 commit ca8724a

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

pre-pull-docker-image/action.yml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ inputs:
3030
initial-delay-seconds:
3131
description:
3232
"Delay before the second attempt; doubles after every further
33-
failed attempt."
33+
failed attempt, capped at 60 seconds."
3434
required: false
3535
default: "5"
3636

@@ -47,6 +47,26 @@ runs:
4747
INITIAL_DELAY_SECONDS: ${{ inputs.initial-delay-seconds }}
4848
run: |
4949
set -u
50+
# The numeric inputs arrive as unvalidated strings; a
51+
# non-numeric value would make the loop's comparisons error and
52+
# never hit the max-attempts break, so misconfigured values
53+
# fall back to the defaults with a warning instead.
54+
max_attempts="${MAX_ATTEMPTS}"
55+
case "${max_attempts}" in
56+
'' | *[!0-9]* | 0)
57+
echo "warning: invalid max-attempts '${max_attempts}';" \
58+
"using 5" >&2
59+
max_attempts=5
60+
;;
61+
esac
62+
delay="${INITIAL_DELAY_SECONDS}"
63+
case "${delay}" in
64+
'' | *[!0-9]*)
65+
echo "warning: invalid initial-delay-seconds '${delay}';" \
66+
"using 5" >&2
67+
delay=5
68+
;;
69+
esac
5070
if [ ! -f "${DOCKERFILE}" ]; then
5171
echo "warning: '${DOCKERFILE}' not found; skipping pre-pull" >&2
5272
exit 0
@@ -64,6 +84,12 @@ runs:
6484
exit 0
6585
fi
6686
case "${image}" in
87+
scratch)
88+
# `scratch` is Docker's reserved empty image; there is
89+
# nothing to pull.
90+
echo "base image is 'scratch'; nothing to pre-pull"
91+
exit 0
92+
;;
6793
*'$'*)
6894
# A reference like `FROM base-${VARIANT}` needs build args
6995
# to resolve, which this action doesn't have.
@@ -74,20 +100,24 @@ runs:
74100
esac
75101
echo "Pre-pulling image: ${image}"
76102
attempt=1
77-
delay="${INITIAL_DELAY_SECONDS}"
78103
while true; do
79104
if docker pull "${image}"; then
80105
break
81106
fi
82-
if [ "${attempt}" -ge "${MAX_ATTEMPTS}" ]; then
107+
if [ "${attempt}" -ge "${max_attempts}" ]; then
83108
echo "warning: pull of ${image} failed after" \
84-
"${MAX_ATTEMPTS} attempts; continuing so the build can" \
109+
"${max_attempts} attempts; continuing so the build can" \
85110
"retry the pull itself" >&2
86111
break
87112
fi
88-
echo "pull attempt ${attempt}/${MAX_ATTEMPTS} failed;" \
113+
echo "pull attempt ${attempt}/${max_attempts} failed;" \
89114
"retrying in ${delay}s" >&2
90115
sleep "${delay}"
91116
attempt=$((attempt + 1))
117+
# Double the delay, capping it so a large `max-attempts`
118+
# cannot produce very long sleeps.
92119
delay=$((delay * 2))
120+
if [ "${delay}" -gt 60 ]; then
121+
delay=60
122+
fi
93123
done

0 commit comments

Comments
 (0)