Skip to content

Commit ab2860b

Browse files
write a custom curl retry method
1 parent e75f743 commit ab2860b

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

.github/workflows/scripts/install-and-build-with-sdk.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,36 @@ log() { printf -- "** %s\n" "$*" >&2; }
1717
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
1818
fatal() { error "$@"; exit 1; }
1919

20+
# Retry configuration
21+
CURL_MAX_RETRIES=5
22+
CURL_RETRY_DELAY=5
23+
CURL_TIMEOUT=300
24+
25+
curl_with_retry() {
26+
local attempt=1
27+
local exit_code=0
28+
29+
while [ $attempt -le $CURL_MAX_RETRIES ]; do
30+
if [ $attempt -gt 1 ]; then
31+
log "Retry attempt $attempt of $CURL_MAX_RETRIES after ${CURL_RETRY_DELAY}s delay..."
32+
sleep $CURL_RETRY_DELAY
33+
fi
34+
35+
# Run curl with connection timeout and max time limits
36+
if curl --connect-timeout 30 --max-time $CURL_TIMEOUT --retry 3 --retry-delay 2 --retry-max-time 60 "$@"; then
37+
return 0
38+
else
39+
exit_code=$?
40+
log "curl failed with exit code $exit_code on attempt $attempt"
41+
fi
42+
43+
attempt=$((attempt + 1))
44+
done
45+
46+
error "curl failed after $CURL_MAX_RETRIES attempts"
47+
return $exit_code
48+
}
49+
2050
# Parse command line options
2151
INSTALL_ANDROID=false
2252
INSTALL_STATIC_LINUX=false
@@ -125,7 +155,7 @@ find_latest_swift_version() {
125155
log "Fetching releases from swift.org API..."
126156

127157
local releases_json
128-
releases_json=$(curl -fsSL "${SWIFT_API_INSTALL_ROOT}/releases.json") || fatal "Failed to fetch Swift releases"
158+
releases_json=$(curl_with_retry -fsSL "${SWIFT_API_INSTALL_ROOT}/releases.json") || fatal "Failed to fetch Swift releases"
129159

130160
# Find all releases that start with the minor version (e.g, "6.1")
131161
# Sort them and get the latest one
@@ -212,7 +242,7 @@ find_latest_sdk_snapshot() {
212242
log "Fetching development snapshots from swift.org API..."
213243

214244
local sdk_json
215-
sdk_json=$(curl -fsSL "${SWIFT_API_INSTALL_ROOT}/dev/${version}/${sdk_name}-sdk.json") || fatal "Failed to fetch ${sdk_name}-sdk development snapshots"
245+
sdk_json=$(curl_with_retry -fsSL "${SWIFT_API_INSTALL_ROOT}/dev/${version}/${sdk_name}-sdk.json") || fatal "Failed to fetch ${sdk_name}-sdk development snapshots"
216246

217247
# Extract the snapshot tag from the "dir" field of the first (newest) element
218248
local snapshot_tag
@@ -400,16 +430,16 @@ download_and_verify() {
400430
local temp_sig="${output_file}.sig"
401431

402432
log "Downloading ${url}"
403-
curl -fsSL "$url" -o "$output_file"
433+
curl_with_retry -fsSL "$url" -o "$output_file"
404434

405435
log "Downloading signature"
406-
curl -fsSL "$sig_url" -o "$temp_sig"
436+
curl_with_retry -fsSL "$sig_url" -o "$temp_sig"
407437

408438
log "Setting up GPG for verification"
409439
local gnupghome
410440
gnupghome="$(mktemp -d)"
411441
export GNUPGHOME="$gnupghome"
412-
curl -fSsL https://swift.org/keys/all-keys.asc | zcat -f | gpg --import - >/dev/null 2>&1
442+
curl_with_retry -fSsL https://swift.org/keys/all-keys.asc | zcat -f | gpg --import - >/dev/null 2>&1
413443

414444
log "Verifying signature"
415445
if gpg --batch --verify "$temp_sig" "$output_file" >/dev/null 2>&1; then
@@ -445,7 +475,7 @@ download_and_extract_toolchain() {
445475

446476
# Check if toolchain is available
447477
local http_code
448-
http_code=$(curl -sSL --head -w "%{http_code}" -o /dev/null "$toolchain_url")
478+
http_code=$(curl_with_retry -sSL --head -w "%{http_code}" -o /dev/null "$toolchain_url")
449479
if [[ "$http_code" == "404" ]]; then
450480
log "Toolchain not found: ${toolchain_filename}"
451481
log "Exiting workflow..."
@@ -579,7 +609,7 @@ install_android_sdk() {
579609
if [[ ! -d "${ANDROID_NDK_HOME:-}" ]]; then
580610
# permit the "--android-ndk" flag to override the default
581611
local android_ndk_version="${ANDROID_NDK_VERSION:-r27d}"
582-
curl -fsSL -o ndk.zip --retry 3 https://dl.google.com/android/repository/android-ndk-"${android_ndk_version}"-"$(uname -s)".zip
612+
curl_with_retry -fsSL -o ndk.zip https://dl.google.com/android/repository/android-ndk-"${android_ndk_version}"-"$(uname -s)".zip
583613
command -v unzip >/dev/null || install_package unzip
584614
unzip -q ndk.zip
585615
rm ndk.zip

0 commit comments

Comments
 (0)