View as Markdown

Scopes with Turborepo

Compute scopes from Turborepo's dependency graph.


If you’re using monorepo tools like Turborepo that have built-in dependency graph analysis, you can use their affected project detection instead of file patterns. This approach is often more accurate because these tools understand your project’s dependency relationships.

To use the manual scopes mechanism, configure Mergify to expect scopes from your CI system:

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

Detecting scopes with Turborepo

Section titled Detecting scopes with Turborepo

Use the turbo run build --dry=json command to determine affected projects and upload them to Mergify.

yaml
name: Detect Scopes
on:
pull_request:
jobs:
detect-scopes:
runs-on: ubuntu-26.04
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v5
with:
# The queue-aware base commit must exist locally: turbo resolves
# the base/head range through git.
fetch-depth: 0
# Every commit, without the file contents of past revisions. Leave
# it out with persist-credentials: false, or if a later step reads
# file history (git blame, git log -p): git fetches each missing
# file on demand, one round trip at a time, with the job's token.
filter: blob:none
- name: Get git refs
id: refs
uses: Mergifyio/gha-mergify-ci@v35
with:
action: scopes-git-refs
- name: Get scopes
id: scopes
env:
HEAD: ${{ steps.refs.outputs.head }}
BASE: ${{ steps.refs.outputs.base }}
run: |
# scopes-git-refs reports an empty base on any event that is neither
# a pull request nor a push. Diffing against an empty revision, or
# letting the build tool fall back to its own default base, reports
# scopes for the wrong range.
[ -n "$BASE" ] || { echo "no base ref reported" >&2; exit 1; }
# A failed turbo piped into jq still exits 0 and writes an empty
# scope list, which the queue reads as "this pull request affects
# nothing", so capture the dry run first.
dry=$(npx turbo run build --dry=json --filter="[$BASE...$HEAD]") || exit 1
scopes=$(printf '%s' "$dry" | jq -r '.packages | join(",")')
echo "scopes=$scopes" >> "$GITHUB_OUTPUT"
- name: Scopes upload
uses: Mergifyio/gha-mergify-ci@v35
with:
action: scopes-upload
scopes: ${{ steps.scopes.outputs.scopes }}

Grant id-token: write to the job, or to the whole workflow. The job then trades its GitHub-signed ID token for a Mergify OIDC CI token, and no MERGIFY_TOKEN is stored:

yaml
permissions:
id-token: write # lets the job ask GitHub for an ID token
contents: read # actions/checkout needs it

A permissions block sets every permission it does not list to none: list the ones your other steps need too.

Mergify refuses the ID token until a trust policy of your organization matches the job. A GitHub organization Owner or a CI Admin writes one under Settings → Security & Access → CI Authentication → Trust Policies on the dashboard. OIDC Authentication covers trust policies, pull requests from forks and refused exchanges. If your workflows store MERGIFY_TOKEN today, move them off it.

Other CIs authenticate with a ci application key, and so do the GitHub Actions jobs OIDC leaves out: runs for pull requests from forks when your organization sends them secrets, and jobs that send scopes from the base branch (pull_request_target, workflow_run, issue_comment). Store the key as the MERGIFY_TOKEN repository secret and set it on the job:

yaml
env:
MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}

A MERGIFY_TOKEN that is set wins over the ID token. For pull requests from forks, set it on those runs only and drop the fork condition from the upload step, as Jobs that keep a key shows. Store the key in your Dependabot secrets too, since runs for Dependabot pull requests read only those.

Using the mergifyio/mergify-ci Buildkite plugin, a first step resolves the merge-queue-aware base and head SHAs and exposes them as meta-data, a second computes the affected projects with turbo run build --dry=json into the mergify-ci.scopes meta-data, and a third uploads them. The upload runs in its own step because the plugin replaces the step’s command, so a step that both detects and uploads never runs its detection:

yaml
steps:
- label: ":mag: Get git refs"
key: git-refs
plugins:
- mergifyio/mergify-ci#v30:
action: scopes-git-refs
- label: ":mag: Detect scopes"
key: detect-scopes
depends_on: git-refs
command: |
# The base meta-data is only published when a base was resolved, so a
# missing key means there is no base to diff against. Check the value
# too: an empty one would send the build tool back to its own default
# base and report scopes for the wrong range.
BASE=$(buildkite-agent meta-data get "mergify-ci.base") || exit 1
HEAD=$(buildkite-agent meta-data get "mergify-ci.head") || exit 1
[ -n "$$BASE" ] || { echo "no base ref reported" >&2; exit 1; }
# A failed turbo piped into jq still exits 0 and writes an empty scope
# list, which the queue reads as "this pull request affects nothing",
# so capture the dry run first.
DRY=$(npx turbo run build --dry=json --filter="[$$BASE...$$HEAD]") || exit 1
SCOPES=$(printf '%s' "$$DRY" | jq -r '.packages | join(",")')
buildkite-agent meta-data set "mergify-ci.scopes" "$$SCOPES"
- label: ":mag: Upload scopes"
depends_on: detect-scopes
plugins:
- mergifyio/mergify-ci#v30:
action: scopes-upload

The plugin reads your application key from MERGIFY_TOKEN in the agent’s environment. Without it the step still runs, but it logs a warning and sends nothing, and the build stays green, so a missing token looks like a pipeline that reports no scopes.

Install the Mergify CLI in your pipeline and export MERGIFY_TOKEN, which a GitHub Actions job granted id-token: write does without. Use mergify ci git-refs to get the merge-queue-aware base and head SHAs and mergify ci scopes-send to upload the detected scopes:

Terminal windowbash
REFS=$(mergify ci git-refs --format json)
# -e so a null ref fails here instead of flowing on as the literal string
# "null": git-refs reports no base outside pull request and push events.
BASE=$(printf '%s' "$REFS" | jq -er '.base') || exit 1
HEAD=$(printf '%s' "$REFS" | jq -er '.head') || exit 1
# A failed turbo piped into jq still exits 0 and writes a well-formed, empty
# scopes.json, which the queue reads as "this pull request affects nothing".
DRY=$(npx turbo run build --dry=json --filter="[$BASE...$HEAD]") || exit 1
# error() rather than a bare .packages: if turbo ever stops reporting that key,
# jq would write {"scopes": null} and exit 0. This block runs in whatever shell
# your CI uses, so it cannot lean on set -e.
printf '%s' "$DRY" \
| jq '{scopes: (.packages // error("turbo reported no packages"))}' > scopes.json || exit 1
mergify ci scopes-send --scopes-json scopes.json

Both commands read the pull request from the environment on GitHub Actions, Buildkite, and a Jenkins multibranch pipeline; Reporting scopes from Jenkins shows a complete stage. On any other CI, git-refs falls back to the last commit, HEAD^..HEAD, which misses the rest of the pull request, and scopes-send finds no pull request number, skips the upload, and still passes. There, diff from the merge base of the pull request's head commit and its target branch, and pass --pull-request with the number and --head-sha with the head commit, not a merge commit your CI built.

Whichever recipe you use, the scopes it uploads are the package names turbo reports under .packages, and a scoped name such as @acme/web can be given a capacity of its own: scopes.capacities takes any name the scopes API accepts, whatever the source. Quote it in YAML, since a key starting with @ is not a plain scalar. A key holding *, ?, [ or { is a glob over scope names instead, so '@acme/*' gives every @acme/ package its own budget. To bound the rest, scopes.default_capacity applies to every scope no key matches. Both keys apply in every merge_queue.mode, including the default serial mode the configuration above leaves the queue in.

Was this page helpful?