View as Markdown

OIDC Authentication for GitHub Actions

Let GitHub Actions jobs reach Mergify with the ID token GitHub signs for them instead of a MERGIFY_TOKEN application key, under trust policies you write.


A GitHub Actions job can reach Mergify without MERGIFY_TOKEN. It asks GitHub for an OpenID Connect (OIDC) ID token and trades it with Mergify for an OIDC CI token that works for one hour, on that job’s repository only, under the trust policies you write. A ci application key instead works on every repository of your organization until it expires or is deleted, and sits in repository secrets, where it can leak from a build log or a workflow change. This page takes a workflow that stores a key to one that stores nothing.

Job GitHub Actions job with id-token: write GitHub GitHub signs an ID token: repository, workflow, event, environment Job->GitHub  asks for an ID token Mergify Mergify checks the signature, the audience and the repository GitHub->Mergify  the client sends it Policy Does a trust policy match the job? Mergify->Policy Token Token for this repository, one hour Policy->Token yes Refused Refused: nothing uploaded Policy->Refused no
A job trades its GitHub ID token for a Mergify OIDC CI token only when one of your trust policies matches the job: green is a token, red a refusal

A Mergify client exchanges the ID token of a job granted id-token: write only when MERGIFY_TOKEN is unset or empty: a key that is set always wins, so write your trust policies before you remove one. The token holds what the matching policies grant on that repository, CI Reporting by default, and the client renews it before its hour is up.

A refusal fails no reporting step: the job runs without a token, so the test framework plugins run the tests with no quarantine, mergify ci junit-process uploads and quarantines nothing, and mergify ci scopes-send sends no scopes.

A pull_request job’s results are filed under the ref GitHub signed for it, its pull request’s merge ref refs/pull/<number>/merge, and still count for that pull request.

  • A client that exchanges. The latest releases of the test framework plugins, of the Mergify CLI, and of mergifyio/gha-mergify-ci. A script calling the API directly sends an ID token meant for the Mergify API URL (https://api.mergify.com, or your install’s) to POST /v1/ci/oidc/exchange.

  • GitHub Actions. Buildkite, Jenkins and CircleCI jobs keep using a ci key.

Move your workflows off MERGIFY_TOKEN

Section titled Move your workflows off MERGIFY_TOKEN

Open Settings → Security & Access → CI Authentication on the dashboard, then Trust Policies. GitHub organization Owners and users holding the CI Admin delegated role write policies there, from the CLI or through the API.

Trust CI on Every Repository creates a policy named CI on every repository: every repository, any workflow, the CI events, any environment. Write a Policy starts narrower, and Trust policies describes each line.

Until your organization has a policy, every exchange is refused.

Let the job request an ID token

Section titled Let the job request an ID token

Grant id-token: write to the job, or to the whole workflow:

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, so list the ones the job’s other steps need as well. mergifyio/gha-mergify-ci and the test framework plugins need nothing more.

A reusable workflow gets at most what its caller grants: give id-token: write to the calling job. A job of the called workflow that declares its own permissions must list id-token: write too.

Remove MERGIFY_TOKEN from the step’s env, the token input from mergifyio/gha-mergify-ci, and --token from mergify commands. A job that runs pytest then needs nothing else:

yaml
jobs:
test:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v5
# Add your setup steps here (Python, Node.js, etc.)
- name: Run tests
run: pytest

The upload step of mergifyio/gha-mergify-ci keeps its other inputs:

yaml
- name: Mergify Test Engine Upload
# A pull request from a fork gets no ID token: skip the upload
if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
uses: mergifyio/gha-mergify-ci@v34
with:
report_path: junit.xml
test_step_outcome: ${{ steps.tests.outcome }}

Here tests is the id of your test step. The fork condition replaces a HAS_MERGIFY_TOKEN guard: once the secret is deleted, that guard skips every upload.

A line that still reads ${{ secrets.MERGIFY_TOKEN }} once you delete the secret gives an empty value, and an empty MERGIFY_TOKEN counts as unset. Stop passing the secret to reusable workflows too: they need id-token: write from their caller, not the key.

The Exchanges tab next to Trust Policies, the CLI and the API list every exchange for 90 days, with the policy that allowed it or why it was refused. On an exchange refused because no trust policy matched, the dashboard’s Allow Jobs Like This opens a new policy filled in with that job’s repository, workflow and event. A token Mergify cannot verify is not listed: read the job’s log.

Workflows run for Dependabot pull requests get an ID token like the repository’s other runs, so they no longer need MERGIFY_TOKEN in your Dependabot secrets either.

Once your repositories show only allowed exchanges, delete the MERGIFY_TOKEN secrets, then the ci keys under Settings → Security & Access → CI Authentication. Keep the keys of your other CIs, and one for the jobs that cannot use OIDC.

A policy reads as a sentence: “Jobs from these repositories running these workflows on these events in these environments get these permissions.” A job gets a token when one policy matches it, and a policy matches when the four lines below do. Policies have no order and none denies: a narrow policy cannot hold back what a broad one allows.

  • Repositories: all repositories, the default; all except the ones you pick; only the ones you pick; or every repository of the visibilities you pick (public, internal or private). All but the third include new repositories as Mergify’s GitHub App gets access to them.

  • Events: the CI events, the default (push, pull_request, merge_group, schedule and workflow_dispatch); the events you pick; or any event.

  • Workflows: any workflow, the default; or the workflow files of the job’s own repository, such as .github/workflows/ci.yml, and reusable workflows, optionally pinned to a ref.

  • Environments: any environment, or none, the default; or the environments you name, which a job in no environment does not match.

Keep these in mind while writing one:

  • pull_request_target, workflow_run and issue_comment are not CI events. They run with the repository’s identity on behalf of people who cannot push to it: add them only if those workflows never run code they were sent.

  • A workflow file of the job’s own repository names the workflow the run started from. A reusable workflow it calls, even one of the same repository (uses: ./.github/workflows/...), is added as a reusable workflow.

  • A workflow file of the job’s own repository matches at any ref, so anyone who can push a branch can edit it. Only a reusable workflow pinned to a protected ref, or an environment whose protection rules GitHub checks before the job starts, keeps them from doing so.

  • A private repository can send write tokens to workflows run for pull requests from forks. Those runs then get an ID token, and a policy that allows pull_request gives the fork’s code a token for your repository.

  • An organization can have up to 100 policies.

  • Deleting or changing a policy revokes no token it allowed: each expires within the hour.

  • Pull requests from forks. GitHub gives their runs no ID token, nor secrets on a public repository, so they report nothing either way (see Pull requests from forks). If your organization sends secrets to workflows run for pull requests from forks, keep the key for those runs only:

    env:
      MERGIFY_TOKEN: ${{ github.event.pull_request && github.event.pull_request.head.repo.full_name != github.repository && secrets.MERGIFY_TOKEN || '' }}
    

    Every other run gets an empty value and exchanges its ID token. Drop the fork condition from the mergifyio/gha-mergify-ci upload step then, so fork runs upload with the key.

  • pull_request_target, workflow_run and issue_comment jobs that send scopes. These jobs run on the base branch, and their OIDC CI token reports scopes only for the commit the job runs on, never for the pull request’s head. Mergify refuses the upload, and mergify ci scopes-send fails the step. Give those jobs a ci key, which expires within 90 days.

tox, docker run and Docker Compose start your tests without the job’s environment. Pass ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN through with the variables the plugin reads: GitHub sets them in a job granted id-token: write, and without them the plugin has no ID token to exchange.

An on-premise Mergify install connected to GitHub Enterprise Server accepts the ID tokens of that server’s GitHub Actions, issued by https://<your-github-host>/_services/token, and none from github.com. The clients request the ID token for the Mergify API URL the job reaches, so set MERGIFY_API_URL (the mergify_api_url input of mergifyio/gha-mergify-ci) to your install’s URL, as you do with a key.

On github.com, Mergify also accepts the issuer GitHub Enterprise Cloud lets an enterprise switch its Actions to, https://token.actions.githubusercontent.com/<enterprise>. Nothing to configure.

When Mergify refuses an exchange, its reason appears in the job log: a test framework plugin prints Mergify refused the job's ID token: <reason> at the end of the run, and mergify ci, which mergifyio/gha-mergify-ci runs, prints the same reason as a warning and carries on. What to do depends on how the reason starts:

  • <organization> has no trust policy: write a trust policy.

  • No trust policy of <organization> matches this job: Exchanges or mergify oidc exchanges show <id> shows which line of each policy refused it. Widen a policy, or use Allow Jobs Like This.

  • Mergify's GitHub App has no access to <repository>: add the repository to the app’s installation in your GitHub organization settings.

  • The ID token was issued for another audience: the job requested its ID token for another Mergify. Set MERGIFY_API_URL to the audience the reason names.

  • This Mergify accepts the ID tokens of GitHub Actions jobs from <issuer> only: the job reached a Mergify that serves another GitHub. Check MERGIFY_API_URL.

MERGIFY_TOKEN is not set, with nothing about an ID token, no Mergify credential found from the CLI, or Mergify token is not set, scopes will not be sent to Mergify API from the scopes upload of mergifyio/gha-mergify-ci. The client did not try an exchange:

  • The job lacks id-token: write. For a reusable workflow, check the calling job.

  • The run is for a pull request from a fork.

  • The tests run in a container that was not passed ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN.

  • The plugin or CLI is not on its latest release.

Mergify could not exchange the job's ID token or could not get the job's ID token. Mergify could not be reached, could not fetch GitHub’s signing keys, or GitHub signed no token. The tests run, but this run’s results are not uploaded and no test is quarantined in it. Run the job again.

This Mergify OIDC CI token was issued to a job that runs on <ref>. Mergify cannot tell that the job tests the commit or pull request it sent scopes for. See Jobs that keep a key.

Was this page helpful?