|
| 1 | +name: Ensure Pages published |
| 2 | +description: >- |
| 3 | + Wait for GitHub's auto "pages build and deployment" of the current gh-pages |
| 4 | + HEAD and rerun it (with backoff) if it fails with a transient error. |
| 5 | +
|
| 6 | +runs: |
| 7 | + using: composite |
| 8 | + steps: |
| 9 | + - shell: bash |
| 10 | + env: |
| 11 | + GH_TOKEN: ${{ github.token }} |
| 12 | + REPO: ${{ github.repository }} |
| 13 | + ATTEMPTS: "5" |
| 14 | + run: | |
| 15 | + set -euo pipefail |
| 16 | +
|
| 17 | + target_sha="$(gh api "repos/$REPO/branches/gh-pages" --jq '.commit.sha')" |
| 18 | + echo "Ensuring Pages is published for gh-pages@$target_sha" |
| 19 | +
|
| 20 | + find_run() { |
| 21 | + gh run list --repo "$REPO" --workflow "pages-build-deployment" \ |
| 22 | + --commit "$target_sha" --limit 1 \ |
| 23 | + --json databaseId,status,conclusion \ |
| 24 | + --jq '.[0] // empty | "\(.databaseId) \(.status) \(.conclusion // "")"' 2>/dev/null || true |
| 25 | + } |
| 26 | +
|
| 27 | + # Block until a run for this SHA reaches "completed"; echo "id conclusion". |
| 28 | + await_completed() { |
| 29 | + for _ in $(seq 1 60); do |
| 30 | + local info id status conclusion |
| 31 | + info="$(find_run)" |
| 32 | + id="$(printf '%s' "$info" | awk '{print $1}')" |
| 33 | + status="$(printf '%s' "$info" | awk '{print $2}')" |
| 34 | + conclusion="$(printf '%s' "$info" | awk '{print $3}')" |
| 35 | + if [ -n "$id" ] && [ "$status" = "completed" ]; then |
| 36 | + printf '%s %s\n' "$id" "$conclusion" |
| 37 | + return 0 |
| 38 | + fi |
| 39 | + sleep 10 |
| 40 | + done |
| 41 | + return 1 |
| 42 | + } |
| 43 | +
|
| 44 | + for attempt in $(seq 1 "$ATTEMPTS"); do |
| 45 | + echo "::group::Pages publish check (attempt $attempt/$ATTEMPTS)" |
| 46 | + if ! result="$(await_completed)"; then |
| 47 | + echo "Timed out waiting for the pages-build-deployment run." >&2 |
| 48 | + echo "::endgroup::" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + run_id="${result%% *}" |
| 52 | + conclusion="${result##* }" |
| 53 | + echo "pages-build-deployment run $run_id -> $conclusion" |
| 54 | + echo "::endgroup::" |
| 55 | +
|
| 56 | + if [ "$conclusion" = "success" ]; then |
| 57 | + echo "Pages published for gh-pages@$target_sha." |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | +
|
| 61 | + if [ "$attempt" -lt "$ATTEMPTS" ]; then |
| 62 | + # Progressive backoff: transient failures are usually account-level |
| 63 | + # Pages throttling under concurrent deploys — give it room to clear. |
| 64 | + backoff=$((attempt * 30)) |
| 65 | + echo "Transient failure; waiting ${backoff}s before rerunning run $run_id…" |
| 66 | + sleep "$backoff" |
| 67 | + gh run rerun "$run_id" --repo "$REPO" || true |
| 68 | + # Let the rerun leave the "completed" state before we poll again. |
| 69 | + for _ in $(seq 1 12); do |
| 70 | + status="$(find_run | awk '{print $2}')" |
| 71 | + [ "$status" != "completed" ] && break |
| 72 | + sleep 5 |
| 73 | + done |
| 74 | + fi |
| 75 | + done |
| 76 | +
|
| 77 | + echo "Pages deployment still failing after $ATTEMPTS attempts for gh-pages@$target_sha." >&2 |
| 78 | + exit 1 |
0 commit comments