| 1 | name: Supersede CI |
| 2 | |
| 3 | # Push runs have separate concurrency groups so Notes can finish without |
| 4 | # cancelling their required code ancestor. Only a changed code tree supersedes |
| 5 | # earlier evidence. Force cancellation releases stuck Windows workers after |
| 6 | # the normal cancellation grace period. |
| 7 | |
| 8 | on: |
| 9 | push: |
| 10 | branches: [main-v2] |
| 11 | |
| 12 | permissions: |
| 13 | actions: write |
| 14 | contents: read |
| 15 | |
| 16 | jobs: |
| 17 | cancel-superseded: |
| 18 | runs-on: ubuntu-latest |
| 19 | timeout-minutes: 10 |
| 20 | steps: |
| 21 | - uses: actions/checkout@v7 |
| 22 | with: |
| 23 | fetch-depth: 0 |
| 24 | |
| 25 | - name: Cancel CI runs this push supersedes |
| 26 | env: |
| 27 | GH_TOKEN: ${{ github.token }} |
| 28 | run: | |
| 29 | set -euo pipefail |
| 30 | runs="$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/ci.yml/runs?branch=main-v2&event=push&per_page=30" \ |
| 31 | --jq '.workflow_runs[] | select(.status != "completed") | "\(.id) \(.head_sha)"')" |
| 32 | [ -n "$runs" ] || { echo "no unfinished ci.yml runs on main-v2"; exit 0; } |
| 33 | while read -r id sha; do |
| 34 | [ -n "$id" ] || continue |
| 35 | if [ "$sha" = "$GITHUB_SHA" ]; then |
| 36 | continue |
| 37 | fi |
| 38 | if ! git merge-base --is-ancestor "$sha" "$GITHUB_SHA"; then |
| 39 | echo "run $id ($sha) is not an ancestor of $GITHUB_SHA; leaving it alone" |
| 40 | continue |
| 41 | fi |
| 42 | code_changes="$(git diff --name-only "$sha" "$GITHUB_SHA" -- . ':!release-notes')" |
| 43 | if [ -z "$code_changes" ]; then |
| 44 | echo "run $id ($sha) provides the unchanged code evidence required by $GITHUB_SHA; preserving" |
| 45 | continue |
| 46 | fi |
| 47 | echo "run $id ($sha) is superseded by $GITHUB_SHA; cancelling" |
| 48 | gh api -X POST "repos/$GITHUB_REPOSITORY/actions/runs/$id/cancel" >/dev/null || true |
| 49 | status="unknown" |
| 50 | for _ in 1 2 3 4 5 6; do |
| 51 | sleep 15 |
| 52 | status="$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$id" --jq .status)" |
| 53 | [ "$status" = "completed" ] && break |
| 54 | done |
| 55 | if [ "$status" != "completed" ]; then |
| 56 | echo "run $id still $status after the normal cancel; force-cancelling" |
| 57 | gh api -X POST "repos/$GITHUB_REPOSITORY/actions/runs/$id/force-cancel" >/dev/null |
| 58 | fi |
| 59 | done <<< "$runs" |
| 60 |