From bb76c3de3b5102a6e7a892dd8928838fdc7bae1f Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Wed, 9 Oct 2024 11:16:07 -0400 Subject: [PATCH] chore(ci): enable retries for choco installations --- .../environment/bootstrap-windows-2019.ps1 | 3 + scripts/util/retry.sh | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 scripts/util/retry.sh diff --git a/scripts/environment/bootstrap-windows-2019.ps1 b/scripts/environment/bootstrap-windows-2019.ps1 index bc3ccfca9681c..c515edbcfd7af 100644 --- a/scripts/environment/bootstrap-windows-2019.ps1 +++ b/scripts/environment/bootstrap-windows-2019.ps1 @@ -12,6 +12,9 @@ if ($env:RELEASE_BUILDER -ne "true") { rustup run stable cargo install cargo-nextest --version 0.9.72 --locked } +# Enable retries to avoid transient network issues. +export NUGET_ENABLE_ENHANCED_HTTP_RETRY=true + # Install some required dependencies / tools. choco install make choco install protoc diff --git a/scripts/util/retry.sh b/scripts/util/retry.sh new file mode 100644 index 0000000000000..6a007feb48f41 --- /dev/null +++ b/scripts/util/retry.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Usage: ./retry.sh -r -d -c "" +# Example: ./retry.sh -r 5 -d 30 -c "choco install protoc" + +max_retries=3 +delay=10 +command="" + +# Parse options +while getopts "r:d:c:" opt; do + case $opt in + r) + max_retries=$OPTARG + ;; + d) + delay=$OPTARG + ;; + c) + command=$OPTARG + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac +done + +# Check if command is provided +if [[ -z "$command" ]]; then + echo "Usage: $0 -r -d -c \"\"" + exit 1 +fi + +attempt=1 + +while [[ $attempt -le $max_retries ]]; do + echo "Attempt $attempt to run: $command" + + if eval "$command"; then + echo "Command succeeded on attempt $attempt." + exit 0 + else + echo "Attempt $attempt failed. Retrying in $delay seconds..." + ((attempt++)) + sleep $delay + delay=$((delay * 2)) # Exponential backoff (increase delay each time) + fi + + if [[ $attempt -gt $max_retries ]]; then + echo "Command failed after $max_retries attempts." + exit 1 + fi +done