·

GitHub Actions Concurrency for Agent Pull Requests

Prepared and checked using AI. Sources are linked in the article.

parallel-development workflow ai-coding

To cancel old GitHub Actions pull request runs per agent branch without canceling checks for other branches, give each PR its own concurrency.group and set cancel-in-progress: true. For a workflow triggered by pull_request, ${{ github.workflow }}-${{ github.ref }} is a practical key: the workflow name separates workflows, while the PR ref separates pull requests. GitHub Actions concurrency cancels running work only when cancel-in-progress is enabled; it also replaces an older pending run in the same group by default.

Add concurrency to the PR workflow

For example, put the following in .github/workflows/agent-pr-ci.yml for a repository whose make test target runs its test suite. The pull_request trigger runs on opened, synchronize, and reopened by default, so listing those activity types makes the intended scope explicit. The checkout action places the repository in the job workspace before the test command runs.

name: Agent PR CI

on:
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: make test

Put concurrency at the workflow level when all jobs in an obsolete run can stop. A newer run in the same group can then cancel the older workflow, including jobs that have already started. Keep the jobs section and test commands appropriate to your repository; the concurrency block is the part that controls cancellation.

Suppose two agents open separate PRs and both push another commit while CI is running. Each PR’s next run to enter its concurrency group replaces work for that PR only. GitHub processes runs according to when they start waiting on the group, which may differ from dispatch or commit order; cancellation does not compare commit freshness. Confirm that the surviving run checks the current PR revision. The group does not impose a repository-wide limit: the two PRs can still have checks running at the same time.

Why the PR ref works

On a pull_request run, github.ref has the form refs/pull/<number>/merge. That value remains associated with the PR as its head branch receives new commits, while another PR has a different number. github.workflow is the workflow name; including it prevents a test workflow from sharing a group with a differently named lint or build workflow in the same repository.

This is per PR, which usually gives the desired per branch behavior when each agent branch has one PR. It also handles two forks that use the same source branch name: their PR refs differ. If one source branch has separate PRs targeting different base branches, those PRs keep independent checks. That is usually what reviewers need, because each PR has its own proposed merge.

Choose the identifier deliberately. github.sha changes with the commit, so putting it in the group would keep obsolete and current commits in separate groups. github.run_id is unique to a run, so it also prevents replacement. github.base_ref names the target branch; using it alone would group every PR aimed at the same target together. github.ref_name is not the agent’s source branch name on a PR run: it uses the PR merge ref’s short form. Use github.head_ref when you specifically need the source branch.

Group by the exact source branch when needed

A branch key is appropriate if several PRs from one agent branch should share a single CI slot. Include the source repository as well as the branch name, because forks can contain identically named branches. The PR head repository is available as github.event.pull_request.head.repo.full_name.

Replace the workflow-level group with:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.head.repo.full_name }}-${{ github.head_ref }}
  cancel-in-progress: true

This key treats a source branch in one repository as one unit, even if it has more than one PR. That is a trade-off: a new run for one of those PRs can cancel a run for another PR from the same branch. If the goal is to preserve each PR’s required checks independently, keep the PR-ref key instead.

Do not use github.head_ref alone in a repository that accepts fork PRs. A contributor’s agent/fix branch and another contributor’s agent/fix branch would otherwise share a group. Group names are also case insensitive, so names that differ only by case do not provide separate concurrency groups.

Keep the cancellation scope where you intend it

Workflow-level and job-level concurrency solve different problems. At workflow level, a new commit can stop the entire obsolete test run. At job level, only jobs that share the job’s group are constrained; another job in the old workflow can continue. Use job-level concurrency when one costly job should be replaceable while other work must finish.

If a job uses a matrix, make its group distinct for each matrix entry. Otherwise, jobs from the same run can compete for one group. For a job with an operating-system matrix, the job-level block could be:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
  cancel-in-progress: true

Place that block under the matrix job, where matrix.os is available. This keeps an Ubuntu job from canceling a macOS job for the same PR while allowing a newer Ubuntu job to replace the old Ubuntu job. If the whole run is obsolete after a push, the simpler workflow-level block is easier to maintain.

Handle workflows with more than one trigger

A PR ref and a normal branch ref are different values. Still, review the triggers before adding concurrency. If a workflow runs on every push and on pull_request, a push to an agent branch can start two kinds of CI run. Separate concurrency groups will not remove that duplicate work. If PR checks are the goal, trigger the workflow on PR activity and restrict any push trigger to the branches that need direct-push checks.

For a workflow that checks PRs and also checks direct pushes to main, use:

on:
  push:
    branches: [main]
  pull_request:
    types: [opened, synchronize, reopened]

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

The event name makes the intended boundary visible in the group. A new push to main replaces an older main push run; a new PR update replaces an older run for that PR. If you add workflow_dispatch later, decide whether a manually started run should replace a push run for the same ref before choosing its key.

A pull_request.branches filter refers to the PR’s target branch, not the agent’s source branch. Use it to limit checks to PRs aimed at a target such as main; do not expect it to select source branches named agent/*. Filtering away a workflow that provides a required check can leave a PR without that check, so review branch rules when changing triggers.

Check the result without guessing from a cancelled run

After updating the workflow, compare runs for two active PRs. GitHub CLI can filter runs by workflow, branch, and event:

gh run list --workflow 'Agent PR CI' --branch agent/search --event pull_request --limit 10
gh run list --workflow 'Agent PR CI' --branch agent/docs --event pull_request --limit 10

The branch names in these commands are examples. Check which PR revision the surviving run covers; an older run being cancelled alone does not establish that the current revision was checked. For the merge decision, gh pr checks shows a PR’s CI status and --required limits the display to required checks:

gh pr checks 123 --required

A cancelled old run is not a passing check for the current PR. Protected branches require their required checks to be successful, skipped, or neutral, so let the run for the current revision finish and confirm its result. If a PR appears blocked, check whether its latest run started, whether the workflow trigger still covers the PR, and whether the required job name matches the workflow’s current job.

If your repository uses a merge queue, keep its required checks in mind too. The queue uses a separate merge_group event; add that trigger to workflows supplying required checks for queued merges. Give merge-group runs a key based on their own ref rather than forcing them into an agent PR’s group. This keeps queue checks from being cancelled by later edits to an unrelated PR.

Where Parallel Code fits

Parallel Code is our free, open-source desktop app for macOS and Linux. It runs several AI coding agents in parallel, each isolated in its own git worktree, so each agent’s branch can become a separate PR. GitHub Actions concurrency then controls CI runs as those PRs change; for a broader branch workflow, see using multiple agents on one repo.

Frequently asked questions

Should I use github.head_ref or github.ref?

Use github.ref for PR-scoped cancellation: each PR gets its own merge ref, including when forks reuse a branch name. Use github.head_ref with the head repository’s full name only when you intentionally want all PRs from one source branch to share a group.

Does cancel-in-progress: false still remove old queued runs?

Yes. The default concurrency behavior replaces an existing pending run when another run enters the same group, but it leaves the currently running one alone. Set cancel-in-progress: true when running checks for an obsolete commit should stop as well.

Can a cancelled check block a protected PR?

A cancelled check does not satisfy a required passing check. Verify that the current PR revision receives the required workflow and that its run completes.

Will this make separate agent PRs run one after another?

No, as long as their group values differ. The PR-ref configuration allows separate PRs to run concurrently while replacing work within each PR as runs enter its group; a constant group or one based only on the target branch would constrain unrelated PR checks.