|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Watch the build runs for a release tag and re-run failed jobs until they all |
| 3 | +# pass. A single tag triggers one build run per product, and transient |
| 4 | +# registry/network blips fail runs that a plain re-run would fix; this saves |
| 5 | +# clicking "Re-run failed jobs" by hand. |
| 6 | +# |
| 7 | +# Every failure is retried up to --max-attempts and then given up on, so a real |
| 8 | +# build break gets retried too. Check the log of anything that gives up. |
| 9 | +# |
| 10 | +# Needs: gh (authenticated) and jq. |
| 11 | + |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +interval=60 |
| 15 | +max_attempts=10 |
| 16 | +dry_run=false |
| 17 | +tag= |
| 18 | + |
| 19 | +usage() { |
| 20 | + cat <<'EOF' |
| 21 | +Usage: watch-builds.sh TAG [options] |
| 22 | +
|
| 23 | + TAG git tag to watch (required), e.g. 26.7.1 |
| 24 | +
|
| 25 | + --interval SECS seconds between polls (default: 60) |
| 26 | + --max-attempts N attempts per run before giving up (default: 10) |
| 27 | + --dry-run list what would be re-run without triggering it |
| 28 | + -h, --help show this help |
| 29 | +EOF |
| 30 | +} |
| 31 | + |
| 32 | +while [ $# -gt 0 ]; do |
| 33 | + case $1 in |
| 34 | + --interval) interval=$2; shift 2 ;; |
| 35 | + --max-attempts) max_attempts=$2; shift 2 ;; |
| 36 | + --dry-run) dry_run=true; shift ;; |
| 37 | + -h|--help) usage; exit 0 ;; |
| 38 | + -*) echo "watch-builds.sh: unknown option: $1" >&2; usage >&2; exit 2 ;; |
| 39 | + *) tag=$1; shift ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +if [ -z "$tag" ]; then |
| 44 | + echo "watch-builds.sh: a TAG is required" >&2 |
| 45 | + usage >&2 |
| 46 | + exit 2 |
| 47 | +fi |
| 48 | + |
| 49 | +for bin in gh jq; do |
| 50 | + command -v "$bin" >/dev/null || { echo "watch-builds.sh: $bin is not on PATH" >&2; exit 1; } |
| 51 | +done |
| 52 | + |
| 53 | +# Attempts already triggered per run id, seeded from each run's real run_attempt |
| 54 | +# so re-runs done by hand in the UI still count towards the cap. |
| 55 | +declare -A attempts |
| 56 | +declare -A gave_up |
| 57 | + |
| 58 | +# A run is worth re-running only if it actually failed. In-progress and |
| 59 | +# successful runs are left alone; a cancelled run was stopped on purpose. |
| 60 | +is_retryable() { |
| 61 | + case $1 in |
| 62 | + failure|timed_out|startup_failure) return 0 ;; |
| 63 | + *) return 1 ;; |
| 64 | + esac |
| 65 | +} |
| 66 | + |
| 67 | +printf 'Watching %s (poll %ss, max-attempts %s%s)\n\n' \ |
| 68 | + "$tag" "$interval" "$max_attempts" "$([ "$dry_run" = true ] && echo ', dry-run')" |
| 69 | + |
| 70 | +while true; do |
| 71 | + # --limit 100 comfortably covers the one-run-per-product fan-out. |
| 72 | + runs=$(gh run list --branch "$tag" --limit 100 \ |
| 73 | + --json databaseId,workflowName,status,conclusion 2>/dev/null || echo '[]') |
| 74 | + |
| 75 | + if [ "$(jq length <<<"$runs")" -eq 0 ]; then |
| 76 | + echo "No runs found for $tag, waiting..." |
| 77 | + sleep "$interval" |
| 78 | + continue |
| 79 | + fi |
| 80 | + |
| 81 | + running=0 succeeded=0 retryable=0 gaveup=0 triggered=0 |
| 82 | + |
| 83 | + while IFS=$'\t' read -r id name status conclusion; do |
| 84 | + if [ "$status" != completed ]; then |
| 85 | + running=$((running + 1)) |
| 86 | + printf ' %-6s %s (%s)\n' wait "$name" "$status" |
| 87 | + continue |
| 88 | + fi |
| 89 | + |
| 90 | + if [ "$conclusion" = success ]; then |
| 91 | + succeeded=$((succeeded + 1)) |
| 92 | + printf ' %-6s %s\n' ok "$name" |
| 93 | + continue |
| 94 | + fi |
| 95 | + |
| 96 | + # Completed but not successful. Only failures are re-run; anything else |
| 97 | + # (e.g. cancelled) is reported and left as-is. |
| 98 | + if [ -z "${gave_up[$id]:-}" ] && ! is_retryable "$conclusion"; then |
| 99 | + printf ' %-6s %s (%s)\n' skip "$name" "$conclusion" |
| 100 | + continue |
| 101 | + fi |
| 102 | + |
| 103 | + if [ -z "${attempts[$id]:-}" ]; then |
| 104 | + attempts[$id]=$(gh run view "$id" --json runAttempt --jq .runAttempt 2>/dev/null || echo 1) |
| 105 | + fi |
| 106 | + |
| 107 | + if [ -n "${gave_up[$id]:-}" ] || [ "${attempts[$id]}" -ge "$max_attempts" ]; then |
| 108 | + gave_up[$id]=1 |
| 109 | + gaveup=$((gaveup + 1)) |
| 110 | + printf ' %-6s %s (gave up after %s attempts: %s)\n' FAIL "$name" "${attempts[$id]}" "$conclusion" |
| 111 | + continue |
| 112 | + fi |
| 113 | + |
| 114 | + retryable=$((retryable + 1)) |
| 115 | + next=$((attempts[$id] + 1)) |
| 116 | + if [ "$dry_run" = true ]; then |
| 117 | + printf ' %-6s %s (would be attempt %s)\n' retry "$name" "$next" |
| 118 | + elif gh run rerun "$id" --failed >/dev/null 2>&1; then |
| 119 | + attempts[$id]=$next |
| 120 | + triggered=$((triggered + 1)) |
| 121 | + printf ' %-6s %s (attempt %s)\n' retry "$name" "$next" |
| 122 | + else |
| 123 | + # A rerun is refused until the run has fully settled; the next poll retries. |
| 124 | + printf ' %-6s %s (rerun deferred)\n' retry "$name" |
| 125 | + fi |
| 126 | + done < <(jq -r 'sort_by(.workflowName)[] | [.databaseId, .workflowName, .status, .conclusion] | @tsv' <<<"$runs") |
| 127 | + |
| 128 | + printf '\n %s | ok %s, waiting %s, retrying %s, gave up %s (triggered %s)\n\n' \ |
| 129 | + "$(date +%H:%M:%S)" "$succeeded" "$running" "$retryable" "$gaveup" "$triggered" |
| 130 | + |
| 131 | + # Nothing running and nothing left to retry means we are done. |
| 132 | + if [ "$running" -eq 0 ] && [ "$retryable" -eq 0 ]; then |
| 133 | + if [ "$gaveup" -gt 0 ]; then |
| 134 | + echo "Done: $gaveup run(s) still failing after $max_attempts attempts. Check their logs." |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + echo "Done: all $succeeded run(s) for $tag passed." |
| 138 | + exit 0 |
| 139 | + fi |
| 140 | + |
| 141 | + sleep "$interval" |
| 142 | +done |
0 commit comments