Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up bazel CI builds via smarter caching #5142

Open
smolkaj opened this issue Feb 22, 2025 · 1 comment
Open

Speed up bazel CI builds via smarter caching #5142

smolkaj opened this issue Feb 22, 2025 · 1 comment
Labels
good-first-issue An issue that could be solved by someone new to the code base. infrastructure Topics related to code style and build and test infrastructure.

Comments

@smolkaj
Copy link
Member

smolkaj commented Feb 22, 2025

The bazel builds we run for continuous integration could likely be sped up significantly by using a smarter caching scheme, like P4Runtime and p4-constraints.

@smolkaj smolkaj added enhancement good-first-issue An issue that could be solved by someone new to the code base. labels Feb 22, 2025
@fruffy fruffy added the infrastructure Topics related to code style and build and test infrastructure. label Feb 22, 2025
smolkaj added a commit that referenced this issue Feb 23, 2025
Fixes #5142.

Signed-off-by: Steffen Smolka <[email protected]>
@Sarthak-Developer-Coder
Copy link

Speeding up Bazel CI builds through smarter caching (#5142) can significantly reduce build times and resource consumption. Here’s how to optimize Bazel’s caching strategy for CI:


1. Enable Remote Caching

Bazel supports remote caching, which allows build outputs to be shared across different CI runs, preventing redundant compilations.

Solution: Use a Remote Cache (e.g., Google Cloud, S3, or BuildBuddy)

Modify your CI configuration to use a remote cache:

bazel build --remote_cache=<your-remote-cache-url> --remote_upload_local_results

If using Google Cloud Storage, configure it as follows:

bazel build --remote_cache=grpc://bazel-cache.example.com

For AWS S3, use:

bazel build --experimental_remote_cache=your-s3-bucket-url

👉 Benefit: Cache reuse across different CI jobs.


2. Use Persistent Disk Caching in CI

To store build artifacts locally, enable disk caching in your CI pipeline:

bazel build --disk_cache=/cache/bazel

In a Docker-based CI, mount a persistent volume for /cache/bazel to ensure caches persist between builds.

👉 Benefit: Faster builds for frequently changed code.


3. Enable Remote Execution (Optional)

If you have a distributed build environment, configure Bazel Remote Execution to offload computations.

bazel build --remote_executor=grpc://build-cluster.example.com

👉 Benefit: Speed up builds by distributing workloads.


4. Reduce Unnecessary Rebuilds

Modify bazel.rc to enable smart dependency tracking:

build --repository_cache=~/.bazel-cache
build --config=ci

Add --config=ci in your CI pipeline:

bazel test --config=ci //...

👉 Benefit: Avoid rebuilding unchanged dependencies.


5. Optimize CI Cache Keys

For GitHub Actions, cache Bazel’s outputs:

- name: Cache Bazel
  uses: actions/cache@v3
  with:
    path: ~/.cache/bazel
    key: ${{ runner.os }}-bazel-${{ github.sha }}
    restore-keys: |
      ${{ runner.os }}-bazel-

👉 Benefit: Restores previous builds for faster execution.


6. Use --keep_state_after_build

Preserve intermediate build state between CI steps:

bazel build --keep_state_after_build

👉 Benefit: Reduces recomputation in multi-step builds.


7. Enable Build Profiling

To identify slow targets, generate a build profile:

bazel build --profile=bazel-profile.json

Analyze with:

bazel analyze-profile bazel-profile.json

👉 Benefit: Identifies bottlenecks in CI builds.


🚀 Next Steps

Would you like help setting this up for a specific CI provider (GitHub Actions, GitLab, Jenkins)? Let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good-first-issue An issue that could be solved by someone new to the code base. infrastructure Topics related to code style and build and test infrastructure.
Projects
None yet
Development

No branches or pull requests

3 participants