Skip to main content

Bitrise Build Cache for Bazel

Abstract

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.

Try it for free

We offer a 30-day free trial at no cost; you don't even need to provide payment information. The trial starts automatically when you set up the Bitrise Build Cache.

Click here to get started with the Bitrise Build Cache. If you don't have a Bitrise account, you will be prompted to create one first before proceeding to set up the Build Cache.

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

Configuring the Bitrise Build Cache for Bazel in the Bitrise CI environment

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

Workflow Editor

bitrise.yml

  1. Log in to Bitrise and select Bitrise CI on the left, then select your project.

  2. Click the Workflows button on the main page.

  3. Add the 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 the Bitrise Build Cache for Bazel in non-Bitrise 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 Workspace ID: open the Workspace settings page and select General settings on the left navigation menu. You can find and copy the slug from there.

  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 activate bazel --cache --cache-push
    
  5. If you have Remote Build Execution enabled for your workspace, you can also use it locally by adding the --rbe flag.

    Enabling RBE locally

    You will need to have the workers set up for your workspace, and the pool configuration in your repository’s .bazelrc file before enabling RBE locally!

Configuring the Bitrise Build Cache for Bazel in local environments

You can use the Bitrise Build Cache for Bazel on any machine: you just need to create a bitrise.bazelrc configuration file that includes the required configuration for the cache endpoints.

  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 Workspace ID: open the Workspace settings page and select General settings on the left navigation menu. You can find and copy the slug from there.

  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. Download the CLI and install it in a temporary location. You will only need to use it to activate the build cache once, or if any of the settings change.

    curl --retry 5 -sSfL 'https://raw.githubusercontent.com/bitrise-io/bitrise-build-cache-cli/main/install/installer.sh' | sh -s -- -b /tmp/bin -d
  5. Activate the Bitrise Build Cache.

    There are a couple of flags you can use to control the configuration, but we recommend these defaults:

    /tmp/bin/bitrise-build-cache activate bazel --cache --cache-push=false

    Pulling from cache

    We recommend only pulling artifacts from the cache to avoid accidentally sharing incorrect cache data due to file modifications during a build.

    For the full list of flags check the CLI’s /tmp/bin/bitrise-build-cache activate bazel --help command.

  6. If you have Remote Build Execution enabled for your workspace, you can also use it locally by adding the --rbe flag.

    Enabling RBE locally

    You will need to have the workers set up for your workspace, and the pool configuration in your repository’s .bazelrc file before enabling RBE locally!

  7. Optionally, add your repository URL in your repository’s root .bazelrc file.

    We recommend doing this to be able to identify your local builds.

    Replace the URL

    Make sure to replace the placeholder URL in the command with your own!

    build --remote_header='x-repository-url=https://github.com/bazelbuild/bazel.git'
    build --bes_header='x-repository-url=https://github.com/bazelbuild/bazel.git'

That's it! You can now run any bazel commands and take advantage of the Bitrise Build Cache. You can check the invocation details through the link printed during builds:

bazel-local-printout.png