From 3b399ab1ebe83f15676dced732c71ca0f0704074 Mon Sep 17 00:00:00 2001 From: Akshay Mankar Date: Tue, 27 Dec 2022 16:59:55 +0100 Subject: [PATCH] hack/bin/upload-image: Retry despite `set -e` Executing `"$@"` within first argument of `if` prevents `set -e` from immediately failing the whole script. It could also be written as `while ! "$@"; do ...`, but then getting status of `"$@"` is more complicated as `! "$@"` has status=0 and overwrites the value of `$?`. --- hack/bin/upload-image.sh | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/hack/bin/upload-image.sh b/hack/bin/upload-image.sh index e49eaca08b1..a070b8661bb 100755 --- a/hack/bin/upload-image.sh +++ b/hack/bin/upload-image.sh @@ -35,29 +35,24 @@ function retry { local maxAttempts=$1 local secondsDelay=1 local attemptCount=1 - local output= shift 1 while [ $attemptCount -le "$maxAttempts" ]; do - output=$("$@") - local status=$? - - if [ $status -eq 0 ]; then + if "$@"; then break - fi - - if [ $attemptCount -lt "$maxAttempts" ]; then - echo "Command [$*] failed after attempt $attemptCount of $maxAttempts. Retrying in $secondsDelay second(s)." >&2 - sleep $secondsDelay - elif [ $attemptCount -eq "$maxAttempts" ]; then - echo "Command [$*] failed after $attemptCount attempt(s)" >&2 - return $status + else + local status=$? + if [ $attemptCount -lt "$maxAttempts" ]; then + echo "Command [$*] failed after attempt $attemptCount of $maxAttempts. Retrying in $secondsDelay second(s)." >&2 + sleep $secondsDelay + elif [ $attemptCount -eq "$maxAttempts" ]; then + echo "Command [$*] failed after $attemptCount attempt(s)" >&2 + return $status + fi fi attemptCount=$((attemptCount + 1)) secondsDelay=$((secondsDelay * 2)) done - - echo "$output" } tmp_link_store=$(mktemp -d)