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.
-
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.
-
Find your Workspace ID. You can do so by navigating to the Workspace's page and find the ID in the URL.
-
Create a file called
.bitrise.bazelrc.tpl
with the following content:Note
Replace
<workspace-slug>
with the Workspace ID you got in the previous step.build --remote_cache=grpcs://pluggable.services.bitrise.io build --remote_header=x-org-id=<workspace-slug> build --remote_header=authorization="Bearer $BITRISE_PERSONAL_ACCESS_TOKEN"
-
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. Thebazel
commands will run but without a build cache if.bitrise.bazelrc
isn’t present. -
Create the
bitrise.bazelrc
file from thebitrise.bazelrc.tpl
template file:export BITRISE_PERSONAL_ACCESS_TOKEN=your-personal-access-token envsubst < .bitrise.bazelrc.tpl > .bitrise.bazelrc
-
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
bitrise.ci.bazelrc
file. -
Configuring the Bitrise build cache endpoints.
-
Add a Script Step to your Workflow.
-
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' export BITRISE_BAZELRC_TEMPLATE_PATH='./.bitrise.ci.bazelrc.tpl' # ============== # 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 wget https://github.com/bazelbuild/bazelisk/releases/download/v1.17.0/bazelisk-linux-amd64 chmod +x bazelisk-linux-amd64 mv bazelisk-linux-amd64 /usr/local/bin/bazel fi # Create .bitrise.ci.bazelrc.tpl file cat <<EOF > $BITRISE_BAZELRC_TEMPLATE_PATH 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 EOF # 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}" fi # Setup cache endpoint case "${BITRISE_DEN_VM_DATACENTER}" in LAS1) export BITRISE_CACHE_ENDPOINT=grpc://las-cache.services.bitrise.io:6666 ;; ATL1) export BITRISE_CACHE_ENDPOINT=grpc://atl-cache.services.bitrise.io:6666 ;; *) export BITRISE_CACHE_ENDPOINT=grpcs://pluggable.services.bitrise.io ;; esac 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.