Files
hermes-agent/.github/workflows/label-rerun.yml
teknium1 013b776ab0 ci(label-rerun): never rerun a CI run whose PR head has moved
Adding ci-reviewed starts a job that waits for the PR's CI run, then reruns
its failed jobs. A push during the wait cancels that run, the waiter sees
"completed", reruns it, and the rerun rejoins the per-PR concurrency group
and cancels the new head's run. The PR is left testing a stale commit (seen
on #131236 and #131246). Skip the rerun when the head moved: the new run's
own event already carries the label. Same guard on the run-e2e job.
2026-10-01 23:49:01 -07:00

149 lines
6.3 KiB
YAML

name: Label rerun
# When the ``ci-reviewed`` label is added to a PR, rerun all failed jobs in
# the latest CI run. This re-evaluates ``review-labels`` (which now sees the
# label) and GitHub reruns the dependent ``all-checks-pass`` gate.
#
# When the ``run-e2e`` label is added, rerun the head commit's CI, Docker and
# Nix runs in full. Their ``detect`` job reads the PR's labels through the API
# (a rerun replays the original event payload), so the rerun turns every slow
# lane on (scripts/ci/classify_changes.py).
#
# ``gh run rerun`` only works on a completed run. Thus this waits when the
# run is still in progress. The wait is now short. The two slowest jobs are
# the 40-minute comment poller and the 45-minute image build. Each one moved
# to its own workflow, so a CI run ends when its required jobs end.
#
# The review comment updates without help. The poller in
# ci-review-comment.yml watches the CI run through the API. It gets the
# rerun results.
on:
pull_request:
types: [labeled]
permissions:
actions: write
pull-requests: read
# Keyed by label so one label's job does not cancel the other's.
concurrency:
group: label-rerun-${{ github.event.pull_request.number }}-${{ github.event.label.name }}
cancel-in-progress: true
jobs:
rerun-review-labels:
name: Rerun review-labels job
if: github.event.label.name == 'ci-reviewed'
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- name: Wait for CI run to finish, then rerun failed jobs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR: ${{ github.event.pull_request.number }}
run: |
set -uo pipefail
# Find the latest CI run for this PR's head SHA.
RUN_INFO=$(gh run list \
--repo "$REPO" \
--commit "$HEAD_SHA" \
--workflow ci.yaml \
--limit 1 \
--json databaseId,status \
--jq '.[0] | "\(.databaseId) \(.status)"' 2>/dev/null || true)
if [ -z "$RUN_INFO" ]; then
echo "No CI run found for this PR — nothing to rerun."
exit 0
fi
# Split "RUN_ID STATUS" into two vars. Read STATUS from RUN_INFO,
# not from the truncated RUN_ID. Both values came from the same
# var before, which made STATUS the run id. Thus the wait branch
# always ran.
RUN_ID="${RUN_INFO%% *}"
STATUS="${RUN_INFO##* }"
echo "Latest CI run: $RUN_ID (status: $STATUS)"
# If the run is still in progress, wait for it to finish.
# gh run rerun only works on completed runs — if we try while it's
# running, GitHub rejects with "cannot be rerun; This workflow is
# already running".
if [ "$STATUS" != "completed" ]; then
echo "Run is $STATUS — waiting for completion (this may take a while)..."
# gh run watch --exit-status exits non-zero if the run fails,
# which is expected (the label gate fails). Don't let that kill
# the workflow — we WANT to rerun failed jobs.
timeout 2100 gh run watch "$RUN_ID" --repo "$REPO" --interval 15 || true
# Verify it's actually completed now.
STATUS=$(gh run view "$RUN_ID" --repo "$REPO" --json status --jq '.status' 2>/dev/null || echo "unknown")
if [ "$STATUS" != "completed" ]; then
echo "Run is still $STATUS after wait — giving up."
exit 0
fi
fi
# A push while we waited cancelled this run (that is the "completed"
# we saw). Rerunning it would rejoin the PR's CI concurrency group and
# cancel the new head's run, which already sees the label.
CURRENT_HEAD=$(gh pr view "$PR" --repo "$REPO" --json headRefOid --jq .headRefOid || true)
if [ "$CURRENT_HEAD" != "$HEAD_SHA" ]; then
echo "PR head moved to $CURRENT_HEAD; not rerunning superseded run $RUN_ID."
exit 0
fi
echo "Run completed. Rerunning all failed jobs..."
gh run rerun "$RUN_ID" --repo "$REPO" --failed || true
echo "Done. GitHub will rerun review-labels and all dependent jobs."
rerun-with-e2e:
name: Rerun CI with every E2E lane
if: github.event.label.name == 'run-e2e'
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Cancel and rerun the head commit's CI, Docker and Nix runs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR: ${{ github.event.pull_request.number }}
run: |
set -uo pipefail
for workflow in ci.yaml docker.yml nix.yml; do
RUN_ID=$(gh run list --repo "$REPO" --commit "$HEAD_SHA" --workflow "$workflow" \
--event pull_request --limit 1 --json databaseId --jq '.[0].databaseId // empty' || true)
if [ -z "$RUN_ID" ]; then
echo "$workflow: no run for $HEAD_SHA"
continue
fi
# A run in progress was classified without the label: stop it,
# then rerun it once GitHub marks it completed.
STATUS=$(gh run view "$RUN_ID" --repo "$REPO" --json status --jq .status || echo unknown)
if [ "$STATUS" != completed ]; then
gh run cancel "$RUN_ID" --repo "$REPO" || true
for _ in $(seq 60); do
STATUS=$(gh run view "$RUN_ID" --repo "$REPO" --json status --jq .status || echo unknown)
[ "$STATUS" = completed ] && break
sleep 5
done
fi
# Same supersede guard as above: never rerun a run for an old head.
CURRENT_HEAD=$(gh pr view "$PR" --repo "$REPO" --json headRefOid --jq .headRefOid || true)
if [ "$CURRENT_HEAD" != "$HEAD_SHA" ]; then
echo "PR head moved to $CURRENT_HEAD; not rerunning superseded $workflow run $RUN_ID."
exit 0
fi
if gh run rerun "$RUN_ID" --repo "$REPO"; then
echo "$workflow: rerunning $RUN_ID"
else
echo "::warning::$workflow: could not rerun $RUN_ID (status $STATUS)"
fi
done