We use cookies to understand how people use Depot.
Cache

Configure Bazel to use Depot Cache

Bazel is a build tool that builds code quickly and reliably. Many large projects use it, including Google, and optimizes for incremental builds with advanced local and remote caching and parallel execution. Bazel supports many different languages and platforms, and is highly configurable, scaling to codebases of any size.

Depot Cache provides a remote cache service that works with Bazel, allowing you to incrementally cache and reuse parts of your builds. This cache is accessible from anywhere, both on your local machine and on CI/CD systems.

Note: You need a Depot API token to authenticate with the cache service.

Local workstation

To configure Bazel to use Depot Cache, set two build flags in your .bazelrc file:

build --remote_cache=https://cache.depot.dev
build --remote_header=authorization=DEPOT_TOKEN

If you are a member of multiple organizations and authenticating with a user token, you must additionally specify which organization to use for cache storage with the x-depot-org header:

build --remote_header=x-depot-org=DEPOT_ORG_ID

After you configure Bazel to use Depot Cache, run your builds as you normally would. Bazel automatically communicates with Depot Cache to fetch and reuse any stored build artifacts from your previous builds.

Local workstation with containerized builds

When building Docker images that contain Bazel workspaces locally, your build needs access to Bazel's remote cache credentials to benefit from caching. Containerized builds execute in isolated environments that require explicit configuration.

Dockerfile configuration

Update your Dockerfile to mount the secret and configure Bazel:

# syntax=docker/dockerfile:1

# ... other Dockerfile instructions

# Create .bazelrc with cache configuration
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
    echo "build --remote_cache=https://cache.depot.dev" >> ~/.bazelrc && \
    echo "build --remote_header=authorization=${DEPOT_TOKEN}" >> ~/.bazelrc && \
    bazel build

Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.

Depot CLI

depot build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .

Docker buildx

docker buildx build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .

Bake

Define the secret in your docker-bake.hcl file:

target "default" {
  context    = "."
  dockerfile = "Dockerfile"
  tags       = ["your-image:tag"]
  secret = [
    {
      type = "env"
      id   = "DEPOT_TOKEN"
    }
  ]
}

Then run the build:

DEPOT_TOKEN=your_token depot bake

Depot GitHub Actions runners

Depot GitHub Actions runners are pre-configured to use Depot Cache with Bazel. Each runner launches with a $HOME/.bazelrc file pre-populated with the connection details for Depot Cache.

You don't need additional configuration. Run your Bazel builds as normal:

jobs:
  build:
    runs-on: depot-ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - run: bazel build //...

To disable automatic configuration, turn off Allow Actions jobs to automatically connect to Depot Cache in your organization settings page. You can then manually configure Bazel as described in the Local workstation section.

Depot GitHub Actions runners with containerized builds

When running containerized builds on Depot GitHub Actions runners, your build needs access to Bazel's remote cache credentials. These credentials aren't automatically available inside your Docker build environment.

Dockerfile configuration

Update your Dockerfile to mount the secret and configure Bazel:

# syntax=docker/dockerfile:1

# ... other Dockerfile instructions

# Create .bazelrc with cache configuration
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
    echo "build --remote_cache=https://cache.depot.dev" >> ~/.bazelrc && \
    echo "build --remote_header=authorization=${DEPOT_TOKEN}" >> ~/.bazelrc && \
    bazel build

Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.

depot/build-push-action

Store the Depot token in a GitHub Secret named DEPOT_TOKEN, then configure your workflow:

- name: Build and push
  uses: depot/build-push-action@v1
  with:
    context: .
    file: ./Dockerfile
    push: true
    tags: your-image:tag
    secrets: |
      "DEPOT_TOKEN=${{ secrets.DEPOT_TOKEN }}"

depot/bake-action

Define the secret in your docker-bake.hcl file:

target "default" {
  context    = "."
  dockerfile = "Dockerfile"
  tags       = ["your-image:tag"]
  secret = [
    {
      type = "env"
      id   = "DEPOT_TOKEN"
    }
  ]
}

Then configure your workflow:

- name: Bake
  uses: depot/bake-action@v1
  with:
    files: docker-bake.hcl
  env:
    DEPOT_TOKEN: ${{ secrets.DEPOT_TOKEN }}

Docker CLI

Store the Depot token in a GitHub Secret named DEPOT_TOKEN, then configure your workflow:

- name: Build
  run: |
    docker buildx build \
      --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN \
      -t your-image:tag .
  env:
    DEPOT_TOKEN: ${{ secrets.DEPOT_TOKEN }}