|
| 1 | +# Reruns Daily Trading Pipeline when GitHub's hosted runner pool fails to |
| 2 | +# acquire a runner (annotation: "not acquired by Runner of type hosted..."). |
| 3 | +# Other failure types are left alone. Guarded to first attempt only to |
| 4 | +# prevent rerun loops. |
| 5 | +name: Auto-rerun on Runner Allocation Failure |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_run: |
| 9 | + workflows: ["Daily Trading Pipeline"] |
| 10 | + types: [completed] |
| 11 | + |
| 12 | +permissions: |
| 13 | + actions: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + rerun: |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.run_attempt == 1 }} |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Detect runner allocation failure |
| 21 | + id: detect |
| 22 | + env: |
| 23 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + REPO: ${{ github.repository }} |
| 25 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | + allocation_failure=false |
| 29 | + job_ids=$(gh api "/repos/$REPO/actions/runs/$RUN_ID/jobs" --jq '.jobs[].id') |
| 30 | + for job_id in $job_ids; do |
| 31 | + annotations=$(gh api "/repos/$REPO/check-runs/$job_id/annotations" 2>/dev/null || echo '[]') |
| 32 | + if echo "$annotations" | jq -e 'any(.[]; .message | test("not acquired by Runner"))' >/dev/null; then |
| 33 | + allocation_failure=true |
| 34 | + echo "Runner allocation failure detected on job $job_id" |
| 35 | + break |
| 36 | + fi |
| 37 | + done |
| 38 | + echo "allocation_failure=$allocation_failure" >> "$GITHUB_OUTPUT" |
| 39 | +
|
| 40 | + - name: Rerun failed jobs |
| 41 | + if: steps.detect.outputs.allocation_failure == 'true' |
| 42 | + env: |
| 43 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + REPO: ${{ github.repository }} |
| 45 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 46 | + run: | |
| 47 | + gh run rerun --repo "$REPO" "$RUN_ID" --failed |
| 48 | + echo "Triggered rerun of run $RUN_ID" |
| 49 | +
|
| 50 | + - name: Skip (not a runner allocation failure) |
| 51 | + if: steps.detect.outputs.allocation_failure != 'true' |
| 52 | + run: echo "Failure not caused by runner allocation; no rerun triggered." |
0 commit comments