Skip to content

PR CI: cancel superseded merge-queue runs of the same PR - #51247

Open
ydshieh wants to merge 1 commit into
mainfrom
fix_pr_ci_merge_queue_concurrency
Open

ydshieh wants to merge 1 commit into
mainfrom
fix_pr_ci_merge_queue_concurrency

Conversation

@ydshieh

@ydshieh ydshieh commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

🤗 Transformers CI

What does this PR do?

When a PR is in the merge queue and another PR lands on main first, the queue rebuilds the PR's merge group and triggers a new PR CI run, but the previous run of the same PR keeps running until it finishes.

Example (2026-11-25), PR #51200:

  • run 45590727573 on gh-readonly-queue/main/pr-51200-21adb7e…: 10:00 → 10:19
  • run 45592892844 on gh-readonly-queue/main/pr-51200-37fdf62…: 10:13 → 10:30

Both runs completed; they overlapped for ~6 minutes.

Why the current concurrency doesn't cancel it

For merge_group events, github.event.pull_request.number is empty, so the workflow-level group falls back to github.ref = refs/heads/gh-readonly-queue/main/pr-<N>-<base_sha>. The base SHA changes on every rebuild, so the new run is in a different group and cancel-in-progress never applies.

At workflow level only the github/inputs/vars contexts are available. No field of the merge_group payload contains the PR number alone, and expressions have no substring/regex function to extract it. Other projects report the same limitation, e.g. dotCMS/core#38143, pyrite-wiki/pyrite#1189.

Change

  • New small job concurrency-key (a few seconds): extracts <N> from the merge-queue ref and outputs merge-queue-<base>-pr-<N>. For all other events it outputs run-<run_id> (unique per run).
  • pr-ci gets a job-level concurrency group built from that output, with cancel-in-progress: true. Job-level concurrency can use needs.* outputs, and its groups apply across runs, so a new merge-queue run of PR N cancels the running pr-ci job of the previous run of PR N.
  • For non-merge-queue events the job-level key is unique per run, so it never cancels anything; the existing workflow-level group is unchanged and still handles those (in particular, pushes to main still don't cancel each other).

Not verified

  • Not tested in a real merge queue.
  • Cancelling the pr-ci job (which calls a reusable workflow) is expected to cancel all jobs inside the reusable workflow; not tested.
  • Runs started before this is merged have no job-level group and won't be cancelled by later runs.

🤖 Generated with Claude Code

For merge_group events, github.ref contains the base SHA, so the workflow-level
concurrency group changes every time the merge queue rebuilds a PR's group and
the previous run is never cancelled. Compute the PR number from the ref in a
small job and use it in a job-level concurrency group on pr-ci.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ydshieh

ydshieh commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

@askserge

@tarekziade tarekziade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some stuff to fix, and I guess we'll need to try it

# group above differs each time the merge queue rebuilds a PR's merge group (e.g. another PR landed on `main`) and the
# previous run of the same PR is not cancelled. No context exposes `<N>` alone and expressions can't extract it, so we
# compute it here and use it in a job-level concurrency group below.
concurrency-key:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't run it on all events, we can use

if: github.event_name == 'merge_group'

# previous run of the same PR is not cancelled. No context exposes `<N>` alone and expressions can't extract it, so we
# compute it here and use it in a job-level concurrency group below.
concurrency-key:
runs-on: ubuntu-22.04

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can use ubuntu-slim to speed up

pr-ci:
needs: concurrency-key
concurrency:
group: ${{ github.workflow }}-${{ needs.concurrency-key.outputs.key }}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need somehting like

group: ${{ github.workflow }}-${{ needs.concurrency-key.outputs.key || format('run-{0}', github.run_id) }}

Without it, every skipped event would get the same group PR CI- and they would cancel each other.

fi

pr-ci:
needs: concurrency-key

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add

if: ${{ !cancelled() && needs.concurrency-key.result != 'failure' }}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants