Integrating GitHub Actions with Mergify
Run CI Insights, Job Selection, and Scopes on GitHub Actions.
GitHub Actions is the CI/CD platform
provided by GitHub. Mergify has native integrations for GitHub Actions covering
CI Insights, Job Selection, and Scopes. Any GitHub Actions job status
can also be referenced from your Mergify
conditions via check-success.
Prerequisites
Section titled Prerequisites-
You’ve set up GitHub Actions in your repository. If you’re new to GitHub Actions, see their official documentation.
-
GitHub Actions is already configured to report job statuses to your pull requests.
-
The Mergify GitHub App is installed in your repository.
Mergify features for GitHub Actions
Section titled Mergify features for GitHub Actions-
CI Insights: collect job metrics, detect flaky tests, and get Slack notifications for CI failures.
-
Job Selection: skip unaffected jobs on pull requests to cut CI spend in monorepos.
-
Scopes: batch related pull requests in the merge queue. Examples for Bazel, Nx, and Turborepo are available.
Job selection
Section titled Job selectionJob Selection is scopes applied to your workflows. The same
scopes block that lets the merge queue batch by scope also tells each job
whether it has work to do, so a pull request that only touches the frontend
does not pay for the API and docs test suites.
This section wires it into GitHub Actions; Job Selection explains how it works on any CI. For smarter batching or parallel mode in the merge queue, scopes already do that on their own. See Scopes.
Running Buildkite or Jenkins instead? The Buildkite and Jenkins integrations cover the same ground. Scopes themselves are CI-agnostic, so for any other provider let us know and see other build tools.
Declare your scopes
Section titled Declare your scopesThe action reads the scopes block of your .mergify.yml to know which areas
of the repository map to each scope name:
scopes: source: files: frontend: include: - apps/web/**/* api: include: - services/api/**/* docs: include: - docs/**/*Workflow outline
Section titled Workflow outlineA GitHub Actions workflow driven by scopes has three parts:
-
Detect scopes using the
gha-mergify-ciaction. Its inputs and outputs are listed in the action reference. -
Reuse the scope outputs to conditionally run jobs.
-
Publish a final status (for example with a
ci-gatejob) if you want one check that reflects all the jobs that ran.
Example workflow
Section titled Example workflowname: CIon: pull_request:
jobs: detect-scopes: runs-on: ubuntu-26.04 permissions: id-token: write contents: read outputs: frontend: ${{ fromJSON(steps.scopes.outputs.scopes).frontend }} api: ${{ fromJSON(steps.scopes.outputs.scopes).api }} docs: ${{ fromJSON(steps.scopes.outputs.scopes).docs }} steps: - uses: actions/checkout@v5
- name: Detect scopes id: scopes uses: Mergifyio/gha-mergify-ci@v33 with: action: scopes
frontend-tests: needs: detect-scopes if: ${{ needs.detect-scopes.outputs.frontend == 'true' }} uses: ./.github/workflows/frontend-tests.yaml secrets: inherit
api-tests: needs: detect-scopes if: ${{ needs.detect-scopes.outputs.api == 'true' }} uses: ./.github/workflows/api-tests.yaml secrets: inherit
docs-tests: needs: detect-scopes if: ${{ needs.detect-scopes.outputs.docs == 'true' }} uses: ./.github/workflows/docs-tests.yaml secrets: inherit
ci-gate: if: ${{ !cancelled() }} needs: - detect-scopes - frontend-tests - api-tests - docs-tests runs-on: ubuntu-26.04 steps: - name: Report status uses: Mergifyio/gha-mergify-ci@v33 with: action: wait-jobs jobs: ${{ toJSON(needs) }}In this workflow:
-
detect-scopescallsgha-mergify-ciwith thescopesaction, which inspects the pull request diff and returns a JSON map of scopes set totrueorfalse. -
Each job checks the scope it cares about before running, reducing redundant builds.
-
The final
ci-gatejob ensures that the aggregated status reflects the actual CI coverage, even if some jobs were skipped. Keepdetect-scopesin itsneeds: without it, a failed scope detection skips every test job andci-gatestill reports success.
detect-scopes also sends the scopes it detected back to Mergify, which reads
them when your scopes source
declares manual beside files.
Its permissions let the action reach Mergify with the job’s ID token, as
explained below. A called workflow that uploads
test results needs id-token: write from its calling job too. wait-jobs
needs no credential.
Two of these jobs also write to the workflow run’s summary page:
detect-scopes lists every scope and whether the pull request matched it, and
ci-gate lists the result of each job it waited for. Here a pull request
touched the frontend and the docs, so api-tests was skipped:
Let your CI reach Mergify
Section titled Let your CI reach Mergify
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:
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.
Use an application key instead
Section titled Use an application key instead
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:
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.
Protecting the branch with ci-gate
Section titled Protecting the branch with ci-gateOnce ci-gate publishes a single status, add it as a required check in your
GitHub branch ruleset so that only pull requests with the relevant jobs executed
can merge.
Was this page helpful?
Thanks for your feedback!