---
title: Bazel Integration with Mergify
description: Report your test results from Bazel to Mergify
---

This guide shows how to upload the JUnit reports of the tests
[Bazel](https://bazel.build/) runs to **Test Engine** from your CI workflow.

## Find Bazel's JUnit reports

`bazel test` writes a JUnit report for every test target, without any
configuration:

```bash
bazel test //...
```

Each target's report is `bazel-testlogs/<package>/<target>/test.xml`: the
target `//cart:cart_test` writes `bazel-testlogs/cart/cart_test/test.xml`.
`bazel-testlogs` is a link into Bazel's output directory, and the upload
follows it. Upload every report with the pattern `bazel-testlogs/**/test.xml`.

What a report holds depends on the test:

- A test whose framework writes JUnit XML to the path Bazel gives it in
  `XML_OUTPUT_FILE` reports each of its test cases.
  [GoogleTest](/test-engine/test-frameworks/googletest) does this, in a
  `cc_test` that depends on `@googletest//:gtest_main`.

- Any other test gets a report that Bazel writes for it: one test case, named
  after the target, which passes or fails with the target as a whole.

## Update your CI workflow

### GitHub Actions

Run the tests in a step with an `id`, and let the upload step decide the
job's result:

```yaml
- name: Run Bazel Tests
  id: tests
  continue-on-error: true
  run: bazel test //...
```

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    report_path: bazel-testlogs/**/test.xml
    test_step_outcome: ${{ steps.tests.outcome }}
```

Key points:

- `if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)`: runs the upload step even when the tests fail, so **Test Engine** gets the full report, and skips it on pull requests from forks, which get no ID token.
- `report_path: bazel-testlogs/**/test.xml`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- `test_step_outcome: ${{ steps.tests.outcome }}`: passes the test step's outcome, so Mergify detects a runner that crashed while the JUnit report looks clean. Give your test step an `id` (here `tests`) and use it in `steps.<id>.outcome`.

If you use a job matrix in your workflow (e.g., to test across multiple versions), every leg reports the same job name, because GitHub Actions gives them all one `GITHUB_JOB`. Set the `job_name` input so **Test Engine** can tell the reports apart:

```yaml
jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
```

Your upload step should look like:

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    job_name: example_matrix (${{ matrix.version }})
    report_path: bazel-testlogs/**/test.xml
    test_step_outcome: ${{ steps.tests.outcome }}
```

> **Note:** the job name is part of a test's identity, so a scheduled workflow has to report the same name as the job running those tests on your pull requests.

To benefit from Test Engine quarantine, you need to add `continue-on-error: true`
in your GitHub Actions step that executes your tests and generates the JUnit file.
The step running the `gha-mergify-ci` action then decides whether the job passes or fails,
taking quarantined tests into account.

With `continue-on-error: true`, the job's result comes from the upload step,
which pull requests from forks skip: add the step that fails the job when the
tests failed and nothing was uploaded, as shown in
[pull requests from forks](/ci-insights/setup/github-actions#pull-requests-from-forks).

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

```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](https://dashboard.mergify.com). [OIDC Authentication](/integrations/gha/oidc) covers trust policies, pull requests from forks and refused exchanges. If your workflows store `MERGIFY_TOKEN` today, [move them off it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).

#### Use an application key instead

Other CIs authenticate with a `ci` [application key](/api/application-keys#ci-reporting), 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](/integrations/gha/oidc#jobs-that-keep-a-key) shows. Store the key in your [Dependabot secrets](/integrations/dependabot#reaching-mergify-from-dependabot-pull-requests) too, since runs for Dependabot pull requests read only those.

### Buildkite

```yaml
steps:
  - label: "Run tests"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          report_path: bazel-testlogs/**/test.xml
```

Key points:

- The plugin runs in the `post-command` hook, so it uploads results **after** your tests finish, even if they fail.
- `report_path: bazel-testlogs/**/test.xml`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- Silent failure detection is automatic: the plugin reads the step's exit code to detect a runner that crashed while the JUnit report looks clean.

If you use a build matrix in your pipeline, each variation reports its own step label as its job name, so the variations are already distinct. Set the `job_name` plugin property when the label is not the name you want **Test Engine** to file the reports under:

```yaml
steps:
  - label: "Flaky detection ({{matrix}})"
    matrix:
      - "3.10"
      - "3.11"
      - "3.12"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          job_name: "Tests ({{matrix}})"
          report_path: bazel-testlogs/**/test.xml
```

> **Note:** the job name is part of a test's identity, so a scheduled pipeline has to report the same name as the step running those tests on your pull requests.

> **Caution: keep the token out of the pipeline you upload.** Set `MERGIFY_TOKEN` in the environment of the agents running your steps: [Buildkite secrets](https://buildkite.com/docs/pipelines/security/secrets/buildkite-secrets), a secrets plugin backed by your own secrets store, or an agent `environment` hook that exports it. Do not put the token in the pipeline itself: `buildkite-agent pipeline upload` interpolates `${MERGIFY_TOKEN}` before the pipeline is stored, so a token written into an `env:` block is uploaded in plain text and stays readable in the Buildkite UI and in build history. The plugin's `token:` property carries it the same way.

When using the Buildkite plugin for Test Engine quarantine, the plugin automatically
detects test failures using the step's exit code. No extra configuration is needed.

The `mergifyio/mergify-ci` plugin runs in the `post-command` hook and reads
`BUILDKITE_COMMAND_EXIT_STATUS` to detect silent failures where the test runner
crashed but the JUnit report appears clean.

### Any CI (Mergify CLI)

Quote the pattern, so that `mergify ci junit-process` expands it rather than
the shell: without `globstar`, bash reads `**` as `*` and misses the reports
at other depths.

Install the [Mergify CLI](/cli/usage) in your pipeline and export `MERGIFY_TOKEN`. Run `mergify ci junit-process` after your tests:

```bash
set -o pipefail
exit_code=0
bazel test //... || exit_code=$?
mergify ci junit-process \
  --test-exit-code "$exit_code" \
  "bazel-testlogs/**/test.xml"
```

Key points:

- `MERGIFY_TOKEN`: export this environment variable with your Mergify application key so `junit-process` can authenticate to the API. On GitHub Actions, a job granted `id-token: write` needs none: it trades its ID token for a Mergify OIDC CI token, as [OIDC Authentication](/integrations/gha/oidc) explains.
- `|| exit_code=$?`: CI shells run with `-e`, so a failing test command on its own line would end the script before the upload. This form keeps going and records the code.
- The script needs bash for `set -o pipefail`: on Jenkins, whose `sh` steps often run `dash`, start it with `#!/bin/bash`.
- `--test-exit-code "$exit_code"`: passes the test runner's exit code, so Mergify detects a runner that crashed while the JUnit report looks clean.
- `junit-process` runs last, so its status becomes the step's: `0` when every failure it found is quarantined, `1` when one is not.
- The branch quarantine is evaluated against is detected from your CI provider, falling back to the checked-out branch. Pass `--tests-target-branch <branch>` only to override it.

## Test names

A test that reports its own test cases is named the way its framework names
them: the GoogleTest test `TEST(CartTest, AddsAnItem)` is reported as
`CartTest.AddsAnItem`, with its source file. These names do not carry the
target, so give tests in two targets distinct names to keep their histories
apart.

A test that Bazel reports for is named after its target's package and name:
the `sh_test` `//cart:smoke_test` is reported as `cart/smoke_test`. Moving or
renaming the target starts a new history for it.

## Verify and review in Test Engine

After pushing these changes:

1. Your CI workflow runs `bazel test`.
2. Bazel writes a report per test target under `bazel-testlogs/`.
3. Mergify uploads the reports to Test Engine.

You can then review your test results, including any failures or flaky tests,
directly in the [Test Engine
dashboard](https://dashboard.mergify.com/test-engine/detection).

## Troubleshooting tips

<ul>
  <li>
    One test case per target: the target's test framework does not write to `XML_OUTPUT_FILE`,
    so Bazel reports the target as a single test.
  </li>

  <li>The CLI provides information about the upload. Check the logs in GitHub Actions.</li>
<li>File paths: Double-check that the output file matches the path used in `report_path`.</li>
<li>
  Authentication: The upload step's log says why Mergify refused the job's ID token, as [OIDC
  troubleshooting](/integrations/gha/oidc#troubleshooting) details. A job that sets
  `MERGIFY_TOKEN` uses that key instead: check that it is valid and granted [CI
  Reporting](/api/application-keys#ci-reporting), or [move the job off
  it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).
</li>
<li>Workflow conditions: If your step is not running, confirm the if condition is actually triggered in your job.</li>
</ul>
