Skip to content

Commit

Permalink
Merge pull request #280 from buildkite-plugins/fix/retry-exit-status
Browse files Browse the repository at this point in the history
Fix eval of exit code returned from docker image pull
  • Loading branch information
pzeballos authored Nov 8, 2024
2 parents 6fe3f75 + 02cc421 commit a559dfb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 2 additions & 3 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ fi

if [[ "${BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL:-false}" =~ ^(true|on|1)$ ]] ; then
echo "--- :docker: Pulling ${image}"
if ! retry "${BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES:-3}" \
docker pull "${image}" ; then
retry_exit_status="$?"
retry "${BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES:-3}" docker pull "${image}" || retry_exit_status="$?"
if [ "${retry_exit_status:-0}" -ne 0 ] ; then
echo "!!! :docker: Pull failed."
exit "$retry_exit_status"
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function retry {
local attempts=1

until "$@"; do
retry_exit_status=$?
local retry_exit_status=$?
echo "Exited with $retry_exit_status"
if (( retries == "0" )); then
return $retry_exit_status
Expand Down
36 changes: 36 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ setup() {
unstub docker
}

@test "Pull image first before running BUILDKITE_COMMAND, success on retries" {
export BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL=true
export BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES=2

stub docker \
"pull image:tag" \
"pull image:tag : echo pulled latest image on retry" \
"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"

run "$PWD"/hooks/command

assert_success
assert_output --partial "Retrying 1 more times..."
assert_output --partial "pulled latest image on retry"
assert_output --partial "ran command in docker"

unstub docker
}

@test "Pull image first before running BUILDKITE_COMMAND, failure on retries" {
export BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL=true
export BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES=2

stub docker \
"pull image:tag" \
"pull image:tag : exit 3"

run "$PWD"/hooks/command

assert_failure 3
assert_output --partial "Retrying 1 more times..."
assert_output --partial "!!! :docker: Pull failed."

unstub docker
}

@test "Runs BUILDKITE_COMMAND with mount-buildkite-agent disabled specifically" {
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
export BUILDKITE_COMMAND="pwd"
Expand Down

0 comments on commit a559dfb

Please sign in to comment.