Skip to main content

Remote build cache for Bazel

To successfully use remote caching for Bazel, you need a bitrise.bazelrc file that contains the necessary configurations to enable the remote build cache for your Bazel project. You can use the remote cache for builds running on Bitrise or in other CI environments.

Configuring remote cache for Bazel in the Bitrise CI environment

On Bitrise, you can use the remote cache for Bazel by simply downloading the Bitrise Build Cache CLI in your build and then running the CLI to enable the cache.

  1. Add a Script Step to your Workflow. This Step should come before any Step that runs a Bazel command that you wish to speed up.

  2. In the Content input, add the following:

    #!/usr/bin/env bash
    set -euxo pipefail
    
    # download Bitrise Build Cache CLI
    curl -sSfL 'https://raw.githubusercontent.com/bitrise-io/bitrise-build-cache-cli/main/install/installer.sh' | sh -s -- -b /tmp/bin -d
    
    # run the CLI to enable Bitrise build cache for Bazel
    /tmp/bin/bitrise-build-cache enable-for bazel

No further configuration is required. During your first build, outputs will be saved to cache. We recommend running a couple of builds to ensure the cache is warmed up.

Configuring remote cache for Bazel in other CI environments

The Bitrise Build Cache does not require using the Bitrise CI. You can use other CI/CD services and still take advantage of remote caching to improve your Bazel build times.

To do so, you need to configure your CI environment to download the Bitrise Build Cache CLI during the build and then run the CLI to enable the Bitrise Build Cache.

  1. Generate a Personal Access Token on Bitrise: Creating a personal access token.

    Copy the value of the token, as you will need it during the process.

  2. Find your Bitrise Workspace ID. You can do so by navigating to the Workspace's page and find the ID in the URL.

    workspace-id.png
  3. Set the following Environment Variables in your CI configuration:

    • BITRISE_BUILD_CACHE_AUTH_TOKEN: The value should be your Personal Access Token.

    • BITRISE_BUILD_CACHE_WORKSPACE_ID: The value should be the Bitrise Workspace slug.

  4. Add the following script to your CI configuration before the step you want to speed up:

    Environment

    Make sure to run the script in the same environment as the Bazel command you want to speed up. For example, if you use multiple Docker containers throughout the build, make sure that the Bitrise Build Cache CLI runs in the same Docker container as the Bazel command.

    #!/usr/bin/env bash
    set -euxo pipefail
    
    # download Bitrise Build Cache CLI
    curl -sSfL 'https://raw.githubusercontent.com/bitrise-io/bitrise-build-cache-cli/main/install/installer.sh' | sh -s -- -b /tmp/bin -d
    
    # run the CLI to enable Bitrise build cache for Bazel
    /tmp/bin/bitrise-build-cache enable-for bazel