Skip to content

Commit 6576bc7

Browse files
[macOS] Added HTTP status code check to download_with_retries (actions#3716)
* Disable exit on error temporary to implement retry logic based on exit code * Check HTTP response code and retry if it's not 200 * Make variables local to not interfere with other scripts Co-authored-by: Mikhail Timofeev <48208649+miketimofeev@users.noreply.github.com>
1 parent 11a951e commit 6576bc7

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

images/macos/provision/utils/utils.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@ download_with_retries() {
1010
local COMPRESSED="$4"
1111

1212
if [[ $COMPRESSED == "compressed" ]]; then
13-
COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME'"
13+
local COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME' -w '%{http_code}'"
1414
else
15-
COMMAND="curl $URL -4 -sL -o '$DEST/$NAME'"
15+
local COMMAND="curl $URL -4 -sL -o '$DEST/$NAME' -w '%{http_code}'"
1616
fi
1717

18-
echo "Downloading $URL..."
18+
echo "Downloading '$URL' to '${DEST}/${NAME}'..."
1919
retries=20
2020
interval=30
2121
while [ $retries -gt 0 ]; do
2222
((retries--))
23-
eval $COMMAND
24-
if [ $? != 0 ]; then
25-
echo "Unable to download $URL, next attempt in $interval sec, $retries attempts left"
26-
sleep $interval
27-
else
28-
echo "$URL was downloaded successfully to $DEST/$NAME"
23+
# Temporary disable exit on error to retry on non-zero exit code
24+
set +e
25+
http_code=$(eval $COMMAND)
26+
exit_code=$?
27+
if [ $http_code -eq 200 ] && [ $exit_code -eq 0 ]; then
28+
echo "Download completed"
2929
return 0
30+
else
31+
echo "Error — Either HTTP response code for '$URL' is wrong - '$http_code' or exit code is not 0 - '$exit_code'. Waiting $interval seconds before the next attempt, $retries attempts left"
32+
sleep 30
3033
fi
34+
# Enable exit on error back
35+
set -e
3136
done
3237

3338
echo "Could not download $URL"

0 commit comments

Comments
 (0)