From 5ec169f2590034cd486723eec563939853c43a33 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 30 Jun 2026 11:51:54 -0400 Subject: [PATCH] Pull over the retry script that just landed in GTMSessionFetcher. Hopefully this will avoid the workflow failures that we see some times and usually go away with a retry. --- .github/workflows/retry.sh | 91 +++++++++++++++++++++++++++++++++++ .github/workflows/swiftpm.yml | 5 +- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100755 .github/workflows/retry.sh diff --git a/.github/workflows/retry.sh b/.github/workflows/retry.sh new file mode 100755 index 000000000..101b9a0a7 --- /dev/null +++ b/.github/workflows/retry.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +# A generic retry script that runs a command and retries it if it fails +# and the output matches a specific pattern. + +set -eu +set -o pipefail + +MAX_RETRIES=1 +PATTERN="Unable to find a device matching the provided destination specifier" +DELAY=10 + +# Parse arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --max-retries) + MAX_RETRIES="$2" + shift 2 + ;; + --pattern) + PATTERN="$2" + shift 2 + ;; + --delay) + DELAY="$2" + shift 2 + ;; + --) + shift + break + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +if [ $# -eq 0 ]; then + echo "Usage: $0 [--max-retries N] [--pattern PATTERN] [--delay SECONDS] -- COMMAND [ARGS...]" + exit 1 +fi + +if [ -z "$PATTERN" ]; then + echo "Error: A non-empty pattern is required to identify transient failures." >&2 + exit 1 +fi + +COMMAND=("$@") + +ATTEMPT=0 +MAX_ATTEMPTS=$((MAX_RETRIES + 1)) + +while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do + ATTEMPT=$((ATTEMPT + 1)) + if [ $ATTEMPT -gt 1 ]; then + echo "Attempt $ATTEMPT of $MAX_ATTEMPTS (Retry $((ATTEMPT - 1)) of $MAX_RETRIES)..." + fi + + # We need to capture the output to check for the pattern, but also stream it. + # We'll use a temp file for the log. + LOG_FILE=$(mktemp) + + # Run the command + set +e + "${COMMAND[@]}" 2>&1 | tee "$LOG_FILE" + STATUS=$? + set -e + + if [ $STATUS -eq 0 ]; then + rm -f "$LOG_FILE" + exit 0 + fi + + # Check if the failure matches the required pattern. + SHOULD_RETRY=false + if grep -q "$PATTERN" "$LOG_FILE"; then + echo "Warning: Command failed and output matched pattern: '$PATTERN'" + SHOULD_RETRY=true + fi + + rm -f "$LOG_FILE" + + if [ "$SHOULD_RETRY" = true ] && [ $ATTEMPT -lt $MAX_ATTEMPTS ]; then + echo "Retrying in $DELAY seconds..." + sleep "$DELAY" + else + exit $STATUS + fi +done + diff --git a/.github/workflows/swiftpm.yml b/.github/workflows/swiftpm.yml index 622c6e65b..53f48f19d 100644 --- a/.github/workflows/swiftpm.yml +++ b/.github/workflows/swiftpm.yml @@ -8,6 +8,7 @@ on: - 'UnitTests/**' - 'Package*.swift' - 'SwiftPMTests/**' + - '.github/workflows/retry.sh' - '.github/workflows/swiftpm.yml' pull_request: branches: [ main ] @@ -16,6 +17,7 @@ on: - 'UnitTests/**' - 'Package*.swift' - 'SwiftPMTests/**' + - '.github/workflows/retry.sh' - '.github/workflows/swiftpm.yml' schedule: # Run the first and fifteenth of every month at 6:12 UTC @@ -71,7 +73,8 @@ jobs: ;; esac - xcodebuild \ + .github/workflows/retry.sh -- \ + xcodebuild \ -scheme GoogleAPIClientForREST-Package \ -configuration ${{ matrix.CONFIGURATION }} \ -destination "${DESTINATION}" \