> ## Documentation Index
> Fetch the complete documentation index at: https://trunk-4cab4936-mintlify-12f37660.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Bazel

> A guide for generating Trunk-compatible test reports with Bazel

You can automatically [detect and manage flaky tests](../../detection/) in your Bazel projects by integrating with Trunk. This document explains how to configure Bazel to output compatible reports that can be uploaded to Trunk for analysis.

## Setup steps

Work through the steps below in order. Once you've finished the last one, you'll be ready to move on to [configure uploads in CI](../ci-providers/).

<Steps>
  <Step title={<a href="#generating-reports">Generate a compatible test report</a>} />

  <Step title={<a href="#report-file-path">Configure the report file path or glob</a>} />

  <Step title={<a href="#try-it-locally">Test uploads locally</a>} />
</Steps>

## Generating Reports

Trunk can parse JSON serialized [Build Event Protocol (BEP) ](https://bazel.build/remote/bep)files to detect flaky tests. You can run tests with Bazel in CI with the `--nobuild_event_json_file_path_conversion` option to produce a serialized BEP file.

### Report File Path

You can specify the path of the generated report through the `build_event_json_file` option:

```sh theme={null}
bazel test <TARGETS> \
    --nobuild_event_json_file_path_conversion \
    --build_event_json_file=build_events.json
```

Trunk can parse the `build_events.json` file to locate your test reports. You will still need to **configure your test runners to output compatible reports**, and you can refer to the guides for [individual test frameworks](./).

<Info>
  Trunk accepts BEP files in both JSON and binary formats. Each output format has its own path-conversion flag, so pick the pair that matches the file you generate:

  * JSON: `--build_event_json_file` with `--nobuild_event_json_file_path_conversion`
  * Binary protobuf: `--build_event_binary_file` with `--nobuild_event_binary_file_path_conversion`

  Pass whichever file you generated to the analytics CLI's `--bazel-bep-path`. The CLI detects the format automatically, so you don't need to tell it which one you used.
</Info>

## Bazel flags

These Bazel flags affect whether Trunk receives complete test reports. Review them before configuring uploads.

### Build Without the Bytes

If your CI environment is set up to [build without the bytes](https://blog.bazel.build/2023/10/06/bwob-in-bazel-7.html), you will need the following flag to pull the reports from the remote execution engine:

```sh theme={null}
--remote_download_regex='.*/test.xml'
```

### Keep Going After Test Failures

Avoid setting `--test_keep_going=false` (or `--notest_keep_going`) if you intend to quarantine tests. Leave it at its default (`true`).

When `--test_keep_going` is `false`, Bazel stops at the first failing test, so later tests never run and never reach the BEP report. If that first failure is a quarantined test, Trunk sees only an already-quarantined failure and marks the run as passing. The tests that never ran might have failed, and the PR merges anyway.

## Try It Locally

### The Validate Command

<CodeGroup>
  ```bash Linux (x64) theme={null}
  SKU="trunk-analytics-cli-x86_64-unknown-linux.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --bazel-bep-path=build_events.json
  ```

  ```bash Linux (arm64) theme={null}
  SKU="trunk-analytics-cli-aarch64-unknown-linux.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --bazel-bep-path=build_events.json
  ```

  ```bash macOS (arm64) theme={null}
  SKU="trunk-analytics-cli-aarch64-apple-darwin.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --bazel-bep-path=build_events.json
  ```

  ```bash macOS (x64) theme={null}
  SKU="trunk-analytics-cli-x86_64-apple-darwin.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --bazel-bep-path=build_events.json
  ```
</CodeGroup>

### Test Upload

Before modifying your CI jobs to automatically upload test results to Trunk, try uploading a single test run manually.

You make an upload to Trunk using the following command:

```sh theme={null}
./trunk-analytics-cli upload --bazel-bep-path=build_events.json \
    --org-url-slug <TRUNK_ORG_URL_SLUG> \
    --token <TRUNK_ORG_TOKEN>
```

### Codeowners and Bazel Targets

If your test cases don't have file paths that can be matched against your CODEOWNERS file, you can pass `--use-bazel-target-for-codeowners` to fall back to the Bazel target name (e.g. `//path/to:target`) when associating test cases to owners:

```sh theme={null}
./trunk-analytics-cli upload --bazel-bep-path=build_events.json \
    --org-url-slug <TRUNK_ORG_URL_SLUG> \
    --token <TRUNK_ORG_TOKEN> \
    --use-bazel-target-for-codeowners
```

When using the [Analytics Uploader GitHub Action](https://github.com/trunk-io/analytics-uploader), set the `use-bazel-target-for-codeowners: true` input alongside `bazel-bep-path`.

## Next Steps

Configure your CI to upload test runs to Trunk. Find the guides for your CI framework below:

<Columns cols={3}>
  <Card title="Azure DevOps Pipelines" href="../ci-providers/azure-devops-pipelines" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/azure.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=ea2db5851d5c6eaede9bbf2615bf21f8" width="1600" height="1000" data-path="assets/_shared/azure.png" />

  <Card title="BitBucket Pipelines" href="../ci-providers/bitbucket-pipelines" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/bitbucket.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=d01da27f368afa13e2d77d7a3d830b46" width="1600" height="1000" data-path="assets/_shared/bitbucket.png" />

  <Card title="BuildKite" href="../ci-providers/buildkite" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/buildkite.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=de6ee912070678c6e89d090d3e1f3486" width="1600" height="1000" data-path="assets/_shared/buildkite.png" />

  <Card title="CircleCI" href="../ci-providers/circleci" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/circle-ci.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=1e78bdef1306ae02c1f6c3396d0e9cee" width="1600" height="1000" data-path="assets/_shared/circle-ci.png" />

  <Card title="Drone CI" href="../ci-providers/droneci" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/drone.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=0f909a48e6e1b77538fd67a3947bdff9" width="1600" height="1000" data-path="assets/_shared/drone.png" />

  <Card title="GitHub Actions" href="../ci-providers/github-actions" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/github.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=e80a0c73815505a86a03d75488952e34" width="1600" height="1000" data-path="assets/_shared/github.png" />

  <Card title="GitLab" href="../ci-providers/gitlab" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/gitlab.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=11240b8bb3041a7b449348b5a49be548" width="1600" height="1000" data-path="assets/_shared/gitlab.png" />

  <Card title="Jenkins" href="../ci-providers/jenkins" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/jenkins.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=7e021fa95b2cdcac9e4f81a8b37e5df6" width="1600" height="1000" data-path="assets/_shared/jenkins.png" />

  <Card title="Semaphore" href="../ci-providers/semaphoreci" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/semaphore.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=f8191f7deb4f20a3c522ce794e05bb64" width="1600" height="1000" data-path="assets/_shared/semaphore.png" />

  <Card title="TeamCity" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/teamcity.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=6b45642f0aac61ff08bdc122a722a3be" width="1600" height="1000" data-path="assets/_shared/teamcity.png" />

  <Card title="Travis CI" href="../ci-providers/travisci" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/travis.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=4a322dd0a76307098b5530628fbdcc2c" width="1600" height="1000" data-path="assets/_shared/travis.png" />

  <Card title="Other CI Providers" href="../ci-providers/otherci" img="https://mintcdn.com/trunk-4cab4936-mintlify-12f37660/EiAtG1K4BXHCqsHT/assets/_shared/other.png?fit=max&auto=format&n=EiAtG1K4BXHCqsHT&q=85&s=bb762e1955f19e3863289f05b0a3bead" width="1600" height="1000" data-path="assets/_shared/other.png" />
</Columns>
