View as Markdown

Scopes

Declare the areas of your codebase once, so the merge queue batches by them and Job Selection skips the CI jobs a pull request does not need.


Scopes name the areas of your codebase that a pull request touches. You declare them once, in the scopes section of .mergify.yml, and every Mergify feature that needs to know what a change affects reads that declaration:

  • The merge queue batches pull requests that touch the same scopes, and merges one right away when the base branch only moved in scopes it does not touch.

  • Job Selection skips the CI jobs whose scopes a pull request does not touch.

Here’s a quick walkthrough of how we use scopes on Mergify’s own monorepo:

How the merge queue batches by scope

Section titled How the merge queue batches by scope

How scopes shape a batch depends on the queue mode.

Batching in serial and isolated modes

Section titled Batching in serial and isolated modes

Mergify prefers the closest match: pull requests that overlap in scope are tested together first, while an unrelated change waits for a later batch unless Mergify needs it to fill the requested batch size.

cluster_preferred Preferred batch PR1 PR #101 Scopes: frontend, api frontend frontend PR1->frontend api api PR1->api PR2 PR #214 Scopes: frontend PR2->frontend PR3 PR #305 Scopes: api, docs PR3->api docs docs PR3->docs PR4 PR #412 Scope: tooling tooling tooling PR4->tooling
Scope-aware batch selection in serial or isolated mode: the three pull requests that share frontend and api form the batch; the tooling change waits

How Pull Requests Are Grouped Into a Batch covers the full order of precedence, including how priority and stacks come before scopes.

In parallel mode, each scope is one lane, shared by every queue rule:

  • A batch takes the pull requests its scopes cover. Mergify batches together pull requests of one queue rule that carry the same scopes, in queue order, up to batch_size. A batch with room left also takes waiting pull requests whose scopes are all among its own. Pull requests carrying no scope are batched only with each other. Pull requests of a stack are batched by the scopes of its bottom pull request.

  • A batch waits on every scope it carries. It is tested on top of the last batch ahead of it in each of its scopes, from any queue rule, and merges only after them. Outside barriers, a batch that shares no scope with the batches ahead of it is tested and merged on its own.

B1 Batch 1 PR #601 (api, frontend) PR #602 (api) B3 Batch 3 PR #604 (api, docs) B1->B3 api B2 Batch 2 PR #603 (docs) B2->B3 docs B1 Batch 1 PR #601 (api, frontend) PR #602 (api) B3 Batch 3 PR #604 (api, docs) B1->B3 api B2 Batch 2 PR #603 (docs) B2->B3 docs
Parallel mode with batch_size: 3. PR #602 carries only api, so it joins batch 1, which carries api and frontend; PR #604 brings docs, which batch 1 lacks, so it gets batch 3, tested once on top of batches 1 and 2

Pull requests spread over many combinations of scopes form small batches, whatever batch_size allows.

How parallel mode works covers the rest of the cycle: how many batches are tested at once, under one max_parallel_checks that every queue rule shares, and what a failing batch holds back.

A pull request gets its scopes in one of these ways:

  • File pattern detection: Define scopes directly in your configuration and Mergify evaluates them itself, matching each pull request’s changed files against your patterns. No CI job or GitHub Action is required for the queue to batch by scope. See file-pattern scopes for a step-by-step setup.

  • Manual upload: Use the gha-mergify-ci GitHub Action, the mergifyio/mergify-ci Buildkite plugin, the REST API, or the mergify ci scopes-send CLI to push scopes computed by your own tooling. There is a build tool guide for Nx, Bazel, Pants, and Turborepo, plus one for any other build tool.

  • Both: declare manual beside files, and your CI adds scopes to those the patterns match. See Files and manual example.

An upload sent through the REST API or the Mergify CLI has to stay within these limits:

  • A scope name is at most 1024 characters.
  • A scope name cannot contain a NUL byte or an unpaired UTF-16 surrogate.
  • An upload carries at most 10,000 scopes.

An upload that breaks any of them is refused in full with a 422 and nothing is stored, so the pull request keeps the stale scopes of an earlier run. Cap a generated name’s length in your own tooling, so an over-long one fails the job that produced it.

Declare scopes at the top level of your .mergify.yml file:

.mergify.ymlyaml
scopes:
source:
files:
frontend:
include:
- apps/web/**/*
api:
include:
- services/api/**/*.py
docs:
include:
- docs/**/*
merge_queue_scope: merge-queue
queue_rules:
- name: default
batch_size: 3
  • scopes.source: selects how scopes are provided.

    • files: map scope names to the file patterns that define them. Each entry accepts include and optional exclude lists of glob patterns.

    • manual: instructs Mergify to expect scopes that your CI uploads with the gha-mergify-ci GitHub Action, the mergifyio/mergify-ci Buildkite plugin, the Mergify CLI, or the REST API. These are the only scopes a pull request gets, and the queue waits for them.

    • files and manual together: a pull request gets the scopes its files match plus the scopes and all_scopes flag uploaded for its head commit, and the queue waits for that upload.

    • null: disables scopes entirely.

  • scopes.merge_queue_scope: optional name of the scope reported on a merge queue batch build, so that CI jobs can tell one from an ordinary pull request build (defaults to merge-queue). It is reported whether or not the queue opened a draft pull request for the batch, and a pull request cannot carry it: the queue drops that name from the scopes a pull request reports for itself. See The merge queue scope.

  • scopes.capacities: optional map of scope name to the number of speculative checks that scope may run at the same time, used to limit per-scope concurrency in any mode. See Limiting concurrency per scope. A key holding *, ?, [, or { is a glob over scope names, with the syntax of the glob patterns a files source uses; any other key names one scope exactly. An exact key may be any name the scopes API accepts, whatever the source: a Bazel package label like //lib/core, a scoped package name like @acme/web, or a name your file patterns declare. The source decides which scopes a pull request carries, and any of them can be capped.

  • scopes.default_capacity: optional capacity applied to every scope that no capacities key matches, so scope sets that change from one pull request to the next stay bounded without you listing each one. Like capacities, it applies in any mode. See Capping scopes you did not list.

  • scopes.barrier_files: optional file filters marking files that impact every scope. Only valid with a files source, alone or beside manual. A pull request that changes a matching file becomes a merge queue barrier. See Declaring a pull request impacts every scope.

.mergify.ymlyaml
scopes:
source:
manual:
queue_rules:
- name: default
batch_size: 5

Your CI uploads every scope, usually the projects a build tool such as Bazel, Nx, Pants, or Turborepo finds affected. It sends them with the scopes-upload action of gha-mergify-ci on GitHub Actions, or of the mergifyio/mergify-ci plugin on Buildkite. With no file patterns to match, scope detection (the scopes action) fails the step on a pull request or a push.

.mergify.ymlyaml
scopes:
source:
files:
frontend:
include:
- apps/web/**/*
manual:

A pull request gets the scopes its changed files match plus those your CI uploads for its head commit, such as the projects a build tool finds through dependencies no pattern sees. With manual declared, the queue waits for the head commit’s upload, so send one for every pull request, even with nothing to add. On GitHub Actions, give the extra scopes, comma-separated, to the scopes action, which sends them with the scopes it detects:

yaml
- uses: Mergifyio/gha-mergify-ci@v33
with:
action: scopes
scopes: ${{ steps.affected.outputs.scopes }}

The Buildkite plugin takes the same scopes option, and the Mergify CLI sends them with mergify ci scopes-send; Marking a barrier on upload says when it needs --head-sha.

Declaring a pull request impacts every scope

Section titled Declaring a pull request impacts every scope

Some pull requests invalidate everything: a build-system configuration change or a shared toolchain bump. Enumerating every scope by hand is brittle, and the list goes stale as soon as your set of scopes changes.

Instead, mark the change as a barrier, a pull request that impacts every scope. Mergify sets an all_scopes flag on it and serializes the queue around it. With file-pattern scopes, declare the files that raise it under scopes.barrier_files; with a manual source, alone or beside files, an upload can set the flag instead.

Barrier files (file-pattern scopes)

Section titled Barrier files (file-pattern scopes)

When your scopes come from file patterns, declare the files that impact every scope under scopes.barrier_files. Whenever a queued pull request changes a matching file, Mergify flags it with all_scopes and treats it as a barrier, with no CI job or manual upload required.

.mergify.ymlyaml
scopes:
source:
files:
frontend:
include:
- apps/web/**/*
api:
include:
- services/api/**/*.py
barrier_files:
include:
- .github/workflows/**
- Makefile
queue_rules:
- name: default
batch_size: 3

With this configuration, a pull request that changes .github/workflows/** or Makefile (files that affect the whole repository) becomes a barrier, whichever scopes it also belongs to.

barrier_files accepts the same include and optional exclude patterns as a scope, and needs at least one include: an exclude-only filter would match every pull request and silently serialize the entire queue, so Mergify rejects it.

A renamed file is matched at both of its paths, so moving a file out of a barrier pattern makes the pull request a barrier too: the barrier file is gone from the location you declared.

You can also manage these patterns from the dashboard: the Barrier files editor on the Scopes page edits the same barrier_files filters.

When you upload scopes yourself, set the all_scopes flag on the pull request. With the gha-mergify-ci GitHub Action or the mergifyio/mergify-ci Buildkite plugin, set all_scopes: true on the scopes-upload action, or on the scopes action when your source declares manual beside files. With the Mergify CLI, pass --all to scopes-send:

Terminal windowbash
mergify ci scopes-send -s build-system --all

On a GitHub Actions pull_request or pull_request_target job, Buildkite, and a Jenkins pull request build, the CLI reads the pull request and its head commit from the build. On CircleCI it reads the head commit only: add --pull-request. Anywhere else, add --pull-request and --head-sha with the pull request’s head commit, as the Any CI recipe explains.

When you upload a scopes JSON file with --scopes-json, the CLI also honors an "all_scopes": true field in the file itself, so your detection tooling can set the flag without touching the command line.

You can send the flag through the REST API as well. This endpoint needs an application key granted CI Reporting, which a key granted Every resource does not hold:

Terminal windowbash
curl -X PUT \
-H "Accept: application/json" \
-H "Authorization: Bearer <your-application-key>" \
-H "Content-Type: application/json" \
-d '{"scopes": ["build-system"], "all_scopes": true}' \
https://api.mergify.com/v1/repos/<owner>/<repository>/commits/<head-sha>/scopes

Send the pull request’s head SHA, which is not always the revision your job checked out: on a pull_request event the default checkout is a merge commit, and an upload keyed on that commit matches no pull request head. The pull-request-number variant of this endpoint is deprecated: send the head SHA to the commit endpoint above instead.

The flag complements the scopes list rather than replacing it: keep reporting the concrete scopes your tooling detected, and set all_scopes: true on top when the change affects every target.

A pull request flagged with all_scopes conflicts with every scope, so the merge queue serializes at it: it shares a batch only with other barrier pull requests and is tested on top of every pull request queued ahead of it, and the pull requests queued behind it merge only after it. Because a barrier defeats parallelism, reserve all_scopes for changes that do need testing against everything. In isolated mode a barrier is tested on its own and the pull requests behind it do not wait for it.

The flag is visible in the merge queue status API and on the draft pull request description created by the queue.

Was this page helpful?