---
title: Scopes
description: 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](/merge-queue) batches pull requests that touch the same scopes, and
  [merges one right away](/merge-queue/direct-merge) when the base branch only moved in scopes it
  does not touch.

- [Job Selection](/ci-insights/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:

<Youtube video="GOqDmZl6EI0" title="How Mergify scopes work in a monorepo" />

## How the merge queue batches by scope

How scopes shape a batch depends on the [queue mode](/merge-queue/queue-modes).

### 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.

```dot class="queue" title="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"
digraph {
  edge [arrowhead=none];

  subgraph cluster_preferred {
    class="batch";
    label="Preferred batch";
    PR1 [label="PR #101\nScopes: frontend, api", class="queued"];
    PR2 [label="PR #214\nScopes: frontend", class="queued"];
    PR3 [label="PR #305\nScopes: api, docs", class="queued"];
  }

  PR4 [label="PR #412\nScope: tooling", class="muted"];

  frontend [shape=oval, class="config"];
  api [shape=oval, class="config"];
  docs [shape=oval, class="config"];
  tooling [shape=oval, class="config"];

  PR1 -> frontend;
  PR1 -> api;
  PR2 -> frontend;
  PR3 -> api;
  PR3 -> docs;
  PR4 -> tooling;
}
```

[How Pull Requests Are Grouped Into a Batch](/merge-queue/batches#how-pull-requests-are-grouped-into-a-batch)
covers the full order of precedence, including how priority and stacks come before scopes.

### Batching in parallel mode

In [parallel mode](/merge-queue/queue-modes#parallel-mode), each scope is one lane, shared by
every [queue rule](/merge-queue/rules):

- **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](/merge-queue/stacks#stack-aware-batching) 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](#barrier-behavior-in-the-queue), a batch that shares no scope with the batches ahead
  of it is tested and merged on its own.

```dot class="queue" narrow="flow" title="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"
digraph {
  B1 [label="Batch 1\nPR #601\n(api, frontend)\nPR #602 (api)", class="pending"];
  B2 [label="Batch 2\nPR #603 (docs)", class="pending"];
  B3 [label="Batch 3\nPR #604 (api, docs)", class="queued"];

  B1 -> B3 [label="api"];
  B2 -> B3 [label="docs"];
  { rank=same; B1; B2; }
}
```

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

[How parallel mode works](/merge-queue/queue-modes#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.

## Scope assignment lifecycle

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](/configuration/scopes/file-patterns) for a step-by-step setup.

- **Manual upload:** Use the [`gha-mergify-ci`](https://github.com/Mergifyio/gha-mergify-ci)
  GitHub Action, the
  [`mergifyio/mergify-ci`](https://github.com/Mergifyio/mergify-ci-buildkite-plugin) Buildkite
  plugin, the [REST API](/api/pull-requests), or the `mergify ci scopes-send` CLI to push scopes
  computed by your own tooling. There is a build tool guide for [Nx](/configuration/scopes/nx),
  [Bazel](/configuration/scopes/bazel), [Pants](/configuration/scopes/pants), and
  [Turborepo](/configuration/scopes/turborepo), plus one for
  [any other build tool](/configuration/scopes/others).

- **Both:** declare `manual` beside `files`, and your CI adds scopes to those the patterns match.
  See [Files and manual example](#files-and-manual-example).

### Limits on an upload

An upload sent through the [REST API](/api/pull-requests) or the [Mergify CLI](/cli/usage) 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.

## Configuration schema

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

```yaml
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
```

### Top-level keys

- `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](/configuration/data-types#globs).

  - `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`](#declaring-a-pull-request-impacts-every-scope) 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](/merge-queue/batches/draft-pull-requests) 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](/configuration/scopes/file-patterns#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](/merge-queue/queue-modes#limiting-concurrency-per-scope). A key holding `*`,
  `?`, `[`, or `{` is a glob over scope names, with the syntax of the [glob
  patterns](/configuration/data-types#globs) 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](/merge-queue/queue-modes#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](#declaring-a-pull-request-impacts-every-scope).

### Manual source example

```yaml
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.

### Files and manual example

```yaml
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@v35
  with:
    action: scopes
    scopes: ${{ steps.affected.outputs.scopes }}
```

The Buildkite plugin takes the same [`scopes` option](/integrations/buildkite#adding-extra-scopes),
and the [Mergify CLI](/cli/usage) sends them with `mergify ci scopes-send`;
[Marking a barrier on upload](#marking-a-barrier-on-upload) says when it needs `--head-sha`.

## 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](#barrier-behavior-in-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)

When your scopes come from [file patterns](/configuration/scopes/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.

```yaml
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.

### Marking a barrier on upload

When you upload scopes yourself, set the `all_scopes` flag on the pull request. With the
[`gha-mergify-ci`](/integrations/gha/reference) GitHub Action or the
[`mergifyio/mergify-ci`](/integrations/buildkite#declaring-a-pull-request-impacts-every-scope)
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](/cli/usage), pass
`--all` to `scopes-send`:

```bash
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](/integrations/jenkins#reporting-scopes-from-jenkins), 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](/configuration/scopes/others#any-ci-mergify-cli) 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](/api/pull-requests) as well. This endpoint needs an
application key granted [CI Reporting](/api/application-keys#ci-reporting), which a key granted
Every resource does not hold:

```bash
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.

### Barrier behavior in the queue

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](/merge-queue/queue-modes#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](/api/merge-queue) and on the draft pull request
description created by the queue.
