Spracherkennung für: .yml vermutete Sprache: Abap {ABAP[75] Cobol[62] SQL[49]} [Methode: maximale Elemente, drei Dimensionen]
name: continuous integration (mathlib forks)
on:
pull_request_target:
branches-ignore:
# ignore tmp branches used by bors
- 'staging.tmp*'
- 'trying.tmp*'
- 'staging*.tmp'
- 'nolints'
paths-ignore:
# pull_request_target runs the workflow and its actions from the target branch, not
# the PR, so PR changes under these dirs can't affect the run — triggering is wasteful.
- '.github/**'
concurrency:
# Collapse duplicate runs for the same commit. The key includes the head sha, so runs
# for *different* commits are never in the same group and so never cancel each other
# by arrival order (that part is taken care of by the `guard` job below). The PR
# number is in the key as well because two PRs can share a single fork branch.
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}
cancel-in-progress: true
# Limit permissions for GITHUB_TOKEN for the entire workflow
permissions:
contents: read
id-token: write
actions: read # Allow get-tools to download the prebuilt tools artifact from master's publish_tools runs
pull-requests: write # Only allow PR comments/labels
# All other permissions are implicitly 'none'
jobs:
guard:
name: Supersede older runs
if: ${{ github.event.pull_request.head.repo.fork && github.repository != 'leanprover-community/mathlib4-nightly-testing' }}
runs-on: ubuntu-latest
timeout-minutes: 5
# Overrides the workflow-level block for this job only; the `build` job below still gets
# the workflow-level permissions.
permissions:
actions: write # cancel this run, and the runs it supersedes
pull-requests: read # read the PR's current head sha
steps:
- name: Cancel this run if superseded, else cancel the runs it supersedes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
PR_NUMBER: ${{ github.event.pull_request.number }}
MY_SHA: ${{ github.event.pull_request.head.sha }}
HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
run: |
set -uo pipefail
# Fail open throughout: every API error below falls through to the build, so a
# mistake costs a duplicate build rather than a PR with no CI.
if ! head_sha="$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq '.head.sha')"; then
echo "::warning::Could not read PR #${PR_NUMBER}; building anyway."
exit 0
fi
if [ "${head_sha}" != "${MY_SHA}" ]; then
# Superseded by another commit. Cancel the whole run.
echo "PR #${PR_NUMBER} is at ${head_sha}; this run is for ${MY_SHA}. Cancelling."
if ! gh api -X POST "repos/${REPO}/actions/runs/${RUN_ID}/cancel"; then
echo "::warning::Could not cancel this run; building anyway."
exit 0
fi
# `cancel` is asynchronous, and returning here would let `build` — which `needs`
# this job — claim a self-hosted runner for a build about to be torn down. Block
# instead; GitHub kills this job within seconds, so the wait only has to outlast
# that. Falling through costs one stale build, which beats a PR with no CI.
for _ in $(seq 12); do
sleep 5
echo "Waiting for the cancellation of run ${RUN_ID} to land..."
done
echo "::warning::Cancellation did not land within 60s; building anyway."
exit 0
fi
# This run is the head, so every other in-flight run for this branch is stale.
# Matched on head sha rather than PR number because the runs API reports no PR
# number for `pull_request_target` runs. Runs for *this* sha are left alone: the
# `concurrency` group above already collapses those, and a second PR sharing this
# fork branch keeps its own run.
if ! stale="$(gh api -X GET "repos/${REPO}/actions/workflows/build_fork.yml/runs" \
-f "branch=${HEAD_BRANCH}" -f per_page=50 |
jq -r --arg r "${HEAD_REPO}" --arg s "${MY_SHA}" --arg me "${RUN_ID}" \
'.workflow_runs[]
| select(.head_repository.full_name == $r
and .status != "completed"
and .head_sha != $s
and (.id | tostring) != $me)
| .id')"; then
echo "::warning::Could not list runs for ${HEAD_REPO}:${HEAD_BRANCH}."
exit 0
fi
for id in ${stale}; do
echo "Cancelling run ${id}, superseded by ${MY_SHA}."
if ! err="$(gh api -X POST "repos/${REPO}/actions/runs/${id}/cancel" 2>&1)"; then
case "${err}" in
# The run reached a terminal state between the listing above and this call.
# That is the outcome being asked for, so it is not worth a warning.
*"HTTP 409"*) echo "Run ${id} had already finished." ;;
*) echo "::warning::Could not cancel run ${id}: ${err}" ;;
esac
fi
done
build:
name: ci (fork)
needs: guard
# `guard` checks the fork/repository condition, hence the `result` test: without it
# the `!cancelled()` below would let every *non-fork* PR build here as well as in
# `build.yml`. `!cancelled()` makes it so an infrastructure failure in `guard` builds
# rather than silently dropping the PR's CI, just as the step itself fails open on API
# errors. A superseded run never reaches this job: `guard` cancels the run,
# ending the job as well.
if: ${{ !cancelled() && needs.guard.result != 'skipped' }}
cache-mode: read
uses: ./.github/workflows/build_template.yml
with:
concurrency_group: ${{ github.workflow }}-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}
pr_branch_ref: ${{ github.event.pull_request.head.sha }}
cache_environment: cache-upload-forks
runs_on: pr
secrets: inherit
[Dauer der Verarbeitung: 0.3 Sekunden, vorverarbeitet 2026-11-18]