Skip to content

Commit a559dfb

Browse files
authored
Merge pull request #280 from buildkite-plugins/fix/retry-exit-status
Fix eval of exit code returned from docker image pull
2 parents 6fe3f75 + 02cc421 commit a559dfb

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

commands/run.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ fi
348348

349349
if [[ "${BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL:-false}" =~ ^(true|on|1)$ ]] ; then
350350
echo "--- :docker: Pulling ${image}"
351-
if ! retry "${BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES:-3}" \
352-
docker pull "${image}" ; then
353-
retry_exit_status="$?"
351+
retry "${BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES:-3}" docker pull "${image}" || retry_exit_status="$?"
352+
if [ "${retry_exit_status:-0}" -ne 0 ] ; then
354353
echo "!!! :docker: Pull failed."
355354
exit "$retry_exit_status"
356355
fi

lib/shared.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function retry {
88
local attempts=1
99

1010
until "$@"; do
11-
retry_exit_status=$?
11+
local retry_exit_status=$?
1212
echo "Exited with $retry_exit_status"
1313
if (( retries == "0" )); then
1414
return $retry_exit_status

tests/command.bats

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,42 @@ setup() {
6666
unstub docker
6767
}
6868

69+
@test "Pull image first before running BUILDKITE_COMMAND, success on retries" {
70+
export BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL=true
71+
export BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES=2
72+
73+
stub docker \
74+
"pull image:tag" \
75+
"pull image:tag : echo pulled latest image on retry" \
76+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"
77+
78+
run "$PWD"/hooks/command
79+
80+
assert_success
81+
assert_output --partial "Retrying 1 more times..."
82+
assert_output --partial "pulled latest image on retry"
83+
assert_output --partial "ran command in docker"
84+
85+
unstub docker
86+
}
87+
88+
@test "Pull image first before running BUILDKITE_COMMAND, failure on retries" {
89+
export BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL=true
90+
export BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES=2
91+
92+
stub docker \
93+
"pull image:tag" \
94+
"pull image:tag : exit 3"
95+
96+
run "$PWD"/hooks/command
97+
98+
assert_failure 3
99+
assert_output --partial "Retrying 1 more times..."
100+
assert_output --partial "!!! :docker: Pull failed."
101+
102+
unstub docker
103+
}
104+
69105
@test "Runs BUILDKITE_COMMAND with mount-buildkite-agent disabled specifically" {
70106
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
71107
export BUILDKITE_COMMAND="pwd"

0 commit comments

Comments
 (0)