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

You can use the remote cache for Bazel on the Bitrise CI by adding our dedicated Step to your Workflow. The Step activates the remote build cache. After it executes, Bazel builds will automatically read from the remote cache and push new entries if it's enabled.

Workflow Editor

bitrise.yml

  1. Open your app on Bitrise.

  2. Click the Workflows button on the main page.

  3. On the Workflows & Pipelines page, find the Workflow you need and click into its row to open the Workflow Editor.

    workflow-and-pipelines.png
  4. Add the Activate Bitrise Build Cache for Bazel Step to your Workflow.

    The Step requires no configuration.

  • Open the bitrise.yml file and add the activate-build-cache-for-bazel Step to your Workflow.

    The Step requires no configuration.

    your-workflow:
      steps:
        - git-clone: {}
        - activate-build-cache-for-bazel:
    

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