View as Markdown

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.

  1. You’ve set up GitHub Actions in your repository. If you’re new to GitHub Actions, see their official documentation.

  2. GitHub Actions is already configured to report job statuses to your pull requests.

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

The action reads the scopes block of your .mergify.yml to know which areas of the repository map to each scope name:

.mergify.ymlyaml
scopes:
source:
files:
frontend:
include:
- apps/web/**/*
api:
include:
- services/api/**/*
docs:
include:
- docs/**/*

A GitHub Actions workflow driven by scopes has three parts:

  1. Detect scopes using the gha-mergify-ci action. Its inputs and outputs are listed in the action reference.

  2. Reuse the scope outputs to conditionally run jobs.

  3. Publish a final status (for example with a ci-gate job) if you want one check that reflects all the jobs that ran.

PR Pull request changes Detect detect-scopes job (gha-mergify-ci) PR->Detect Config Scopes config (.mergify.yml) Config->Detect Frontend frontend-tests (run) Detect->Frontend scope: frontend Docs docs-tests (run) Detect->Docs scope: docs API api-tests (skipped) Detect->API scope: api (false) Gate ci-gate (optional) Frontend->Gate Docs->Gate PR Pull request changes Detect detect-scopes job (gha-mergify-ci) PR->Detect Config Scopes config (.mergify.yml) Config->Detect Frontend frontend-tests (run) Detect->Frontend scope: frontend Docs docs-tests (run) Detect->Docs scope: docs API api-tests (skipped) Detect->API scope: api (false) Gate ci-gate (optional) Frontend->Gate Docs->Gate
detect-scopes runs first, and each test job runs only when its scope changed
yaml
name: CI
on:
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-scopes calls gha-mergify-ci with the scopes action, which inspects the pull request diff and returns a JSON map of scopes set to true or false.

  • Each job checks the scope it cares about before running, reducing redundant builds.

  • The final ci-gate job ensures that the aggregated status reflects the actual CI coverage, even if some jobs were skipped. Keep detect-scopes in its needs: without it, a failed scope detection skips every test job and ci-gate still 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:

Run summary: detect-scopes matched docs and frontend, not api; ci-gate shows api-tests skipped, the rest passed

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.

Protecting the branch with ci-gate

Section titled Protecting the branch with ci-gate

Once 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?