> ## Documentation Index
> Fetch the complete documentation index at: https://trunk-4cab4936-sam-gutentag-api-conventions.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="#disable-retries">Disable retries for better detection accuracy</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:

```sh theme={null}
bazel test <TARGETS> \
    --nobuild_event_json_file_path_conversion
```

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

  * `--build_event_json_file` for JSON
  * `--build_event_binary_file` for the binary protobuf encoding

  Pass either file 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>

### 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'
```

### Disable Retries

You need to disable automatic retries if you previously enabled them for more accurate detection results.

Disable retries if you're retrying tests using the `--flaky_test_attempts` command line option or retrying in your test runner.

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

## 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-sam-gutentag-api-conventions/RMA2tHeMVebmkls4/assets/azure.png?fit=max&auto=format&n=RMA2tHeMVebmkls4&q=85&s=a39579188faffced39b8dc77bdfd6bca" width="1600" height="1000" data-path="assets/azure.png" />

  <Card title="BitBucket Pipelines" href="../ci-providers/bitbucket-pipelines" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/RMA2tHeMVebmkls4/assets/bitbucket.png?fit=max&auto=format&n=RMA2tHeMVebmkls4&q=85&s=03a7fab310505fe2a0224bfcec42fb14" width="1600" height="1000" data-path="assets/bitbucket.png" />

  <Card title="BuildKite" href="../ci-providers/buildkite" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/RMA2tHeMVebmkls4/assets/buildkite.png?fit=max&auto=format&n=RMA2tHeMVebmkls4&q=85&s=72ec38bf82f5c90372f2ec160a968bfd" width="1600" height="1000" data-path="assets/buildkite.png" />

  <Card title="CircleCI" href="../ci-providers/circleci" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/RMA2tHeMVebmkls4/assets/circle-ci.png?fit=max&auto=format&n=RMA2tHeMVebmkls4&q=85&s=60d9700c281d5bc07eab3c619f9b28f9" width="1600" height="1000" data-path="assets/circle-ci.png" />

  <Card title="Drone CI" href="../ci-providers/droneci" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/Jp89JT8l5x9D9wH9/assets/drone.png?fit=max&auto=format&n=Jp89JT8l5x9D9wH9&q=85&s=3b7f8bdddf4ef90ff3255320e231f502" width="1600" height="1000" data-path="assets/drone.png" />

  <Card title="GitHub Actions" href="../ci-providers/github-actions" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/Jp89JT8l5x9D9wH9/assets/github.png?fit=max&auto=format&n=Jp89JT8l5x9D9wH9&q=85&s=a337f701a50a3701ed721802eec22f50" width="1600" height="1000" data-path="assets/github.png" />

  <Card title="GitLab" href="../ci-providers/gitlab" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/Jp89JT8l5x9D9wH9/assets/gitlab.png?fit=max&auto=format&n=Jp89JT8l5x9D9wH9&q=85&s=aa3b7b6c434a561525db2babdfa6494e" width="1600" height="1000" data-path="assets/gitlab.png" />

  <Card title="Jenkins" href="../ci-providers/jenkins" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/_tZbo39T-AVwvieD/assets/jenkins.png?fit=max&auto=format&n=_tZbo39T-AVwvieD&q=85&s=a7ff501092de3489d3c8c824236a0bbc" width="1600" height="1000" data-path="assets/jenkins.png" />

  <Card title="Semaphore" href="../ci-providers/semaphoreci" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/6EPZ5IXH1__vFgXF/assets/semaphore.png?fit=max&auto=format&n=6EPZ5IXH1__vFgXF&q=85&s=15ae9dcf3069cf1fb1cd4987d2670bf8" width="1600" height="1000" data-path="assets/semaphore.png" />

  <Card title="TeamCity" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/6EPZ5IXH1__vFgXF/assets/teamcity.png?fit=max&auto=format&n=6EPZ5IXH1__vFgXF&q=85&s=0fc0357e6794a526e6299175baf3d539" width="1600" height="1000" data-path="assets/teamcity.png" />

  <Card title="Travis CI" href="../ci-providers/travisci" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/FjXJyQtUuAxL45aC/assets/travis.png?fit=max&auto=format&n=FjXJyQtUuAxL45aC&q=85&s=3d1249250b29bb351eec3f66f3405841" width="1600" height="1000" data-path="assets/travis.png" />

  <Card title="Other CI Providers" href="../ci-providers/otherci" img="https://mintcdn.com/trunk-4cab4936-sam-gutentag-api-conventions/6EPZ5IXH1__vFgXF/assets/other.png?fit=max&auto=format&n=6EPZ5IXH1__vFgXF&q=85&s=20cc970bad6132744e02a7bafedd4de4" width="1600" height="1000" data-path="assets/other.png" />
</Columns>
