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 your local machine(s) or for builds running in a CI environment.

Configuring remote cache for Bazel in local builds

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. You can do so by navigating to the Workspace's page and find the ID in the URL.

  3. Create a file called .bitrise.bazelrc.tpl with the following content:


    Replace <workspace-slug> with the Workspace ID you got in the previous step.

    build --remote_cache=grpcs://
    build --remote_header=x-org-id=<workspace-slug>
    build --remote_header=authorization="Bearer $BITRISE_PERSONAL_ACCESS_TOKEN"
  4. Add the following line to your .bazelrc configuration file:

    try-import %workspace%/.bitrise.bazelrc

    With this configuration, .bitrise.bazelrc will be loaded if present, but won’t cause issues if it isn’t present. The bazel commands will run but without a build cache if .bitrise.bazelrc isn’t present.

  5. Create the bitrise.bazelrc file from the bitrise.bazelrc.tpl template file:

    export BITRISE_PERSONAL_ACCESS_TOKEN=your-personal-access-token
    envsubst < .bitrise.bazelrc.tpl > .bitrise.bazelrc
  6. Add the bitrise.bazelrc file to .gitignore.

    This is a security measure: the file includes your personal access token so it should not be visible to the public.

That's it! You can now run any bazel commands and take advantage of the Bitrise build cache.

Configuring remote cache for Bazel in the Bitrise CI environment

Configuring the remote cache for Bazel involves:

  • Creating a file.

  • Configuring the Bitrise build cache endpoints.

  1. Add a Script Step to your Workflow.

  2. In the Content input, add the following:

    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a non-zero status, or zero if all commands exit successfully
    set -o pipefail
    # debug log
    set -x
    # === CONFIG ===
    export BAZELRC_PATH='./.bazelrc'
    # ==============
    # Install needed tools for Bazel (only needed on Linux)
    # Note: this isn't needed for Bitrise Build Cache specifically, if you install bazelisk or bazel in a different way yourself feel free to remove this section
    if [ $(uname) == "Linux" ]; then
      apt-get update
      apt-get install -y gettext-base
      chmod +x bazelisk-linux-amd64
      mv bazelisk-linux-amd64 /usr/local/bin/bazel
    # Create file
    build --remote_cache=\$BITRISE_CACHE_ENDPOINT
    build --remote_header=authorization="Bearer \$BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN"
    build --remote_header=x-app-id=\$BITRISE_APP_SLUG
    build --remote_header=x-flare-buildtool=bazel
    build --remote_header=x-flare-builduser=bitrise
    build --remote_header=x-flare-build-id=\$BITRISE_BUILD_SLUG
    build --remote_header=x-flare-step-id=\$BITRISE_STEP_EXECUTION_ID
    # Check if .bazelrc already has the necessary try-import
    # If not, then add it
    if ! grep -q 'try-import %workspace%/.bitrise.bazelrc' "${BAZELRC_PATH}"; then
      # Add a new-line to .bazelrc file just to be sure we're not appending to the last line
      echo '' >> "${BAZELRC_PATH}"
      # Add the .bitrise.bazelrc import in a non breaking way
      echo 'try-import %workspace%/.bitrise.bazelrc' >> "${BAZELRC_PATH}"
    # Setup cache endpoint
    export BITRISE_CACHE_ENDPOINT=grpc://
    export BITRISE_CACHE_ENDPOINT=grpc://
    export BITRISE_CACHE_ENDPOINT=grpcs://
    envsubst < "${BITRISE_BAZELRC_TEMPLATE_PATH}" > .bitrise.bazelrc
    echo "selected cache endpoint: ${BITRISE_CACHE_ENDPOINT}"

    This script creates a .bitrise.bazelrc file and modifies the specified .bazelrc file to try to import it (unless the try-import is already in the .bazelrc). The .bitrise.bazelrc file has all the necessary configurations to enable Bitrise build cache for Bazel for the project, and to eliminate latency, it selects the closest cache server depending on which Bitrise data center the build is running in.