Build a monorepo build: remote cache, sandboxing, and hermeticity.
- User
Build a monorepo build: remote cache, sandboxing, and hermeticity.
- Assistant
Building a Monorepo with Remote Cache, Sandboxing, and Hermeticity Using Bazel
Bazel is the ideal tool for this: it's designed for monorepos (used by Google, Meta, etc.), provides sandboxing (isolated build actions), hermeticity (self-contained builds with declared dependencies only), and remote caching (via HTTP or gRPC). Builds are reproducible across machines.
I'll guide you through:
- Setup (local + remote cache).
- Sample monorepo (multi-language: Go, Python, JS).
- Key configs for the features.
- Verification steps.
Prerequisites
- Linux/macOS (Bazel sandboxing works best on Linux; use
bazeliskfor cross-platform). - Docker (for remote cache setup).
- Git.
Step 1: Install Bazel
Use Bazelisk (auto-downloads correct Bazel version):
npm install -g @bazel/bazelisk # Or brew install bazelisk on macOS # Alias bazel -> bazelisk ln -s $(which bazelisk) /usr/local/bin/bazelStep 2: Set Up Remote Cache
Use BuildBuddy free tier for HTTP remote cache (no auth needed for open projects).
- Sign up at app.buildbuddy.io, create project.
- Get your cache
instance_name(e.g.,gs://your-bucketor their endpoint).
Or self-host with bazel-remote:
docker run --rm -it -p 8080:8080 gcr.io/bazel-public/bazel-remote:latest \ --in_memory \ --http_listen :8080 \ --cas_storage_path /cacheRemote endpoint:
http://localhost:8080.Step 3: Create the Monorepo
mkdir monorepo && cd monorepo git initRoot Files
.bazelrc(global flags for sandboxing + hermeticity + remote cache):# Sandboxing: Isolate actions (no undeclared FS/network access) build --spawn_strategy=sandboxed build --sandbox_debug # Optional: Debug sandbox issues build --sandbox_add_mount_pair=/etc/localtime,/etc/localtime # Allow timezone # Hermeticity: Strict deps, no implicit tools build --distinct_host_configuration=false # Host/tools are hermetic build --incompatible_disallow_empty_glob # Enforce declared deps # Remote Cache (replace with your endpoint) build:remote --remote_cache=http://localhost:8080 # Or BuildBuddy: https://remote.buildbuddy.io?instance_name=your-instance build:remote --google_default_credentials # If using GCS build:remote --remote_upload_local_results=true # Caching + parallelism build --keep_going # Build as much as possible build --jobs=50 # ParallelismWORKSPACE(orWORKSPACE.bazel):workspace(name = "monorepo") # Rules for languages (hermetic: downloads pinned versions) load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # Go http_archive( name = "io_bazel_rules_go", sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa", urls = ["https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip"], ) load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") go_rules_dependencies() go_register_toolchains(version = "1.19.4") # Python http_archive( name = "rules_python", sha256 = "c68bb7ec9bcd0e4c3f4a1e3350d57d0f828a8e2ed6a4d4b0a23aa1e6e36f6a95", strip_prefix = "rules_python-0.25.0", url = "https://github.com/bazelbuild/rules_python/releases/download/0.25.0/rules_python-0.25.0.tar.gz", ) load("@rules_python//python:repositories.bzl", "python_register_toolchains") python_register_toolchains( name = "python3_11", python_version = "3.11", ) # Node.js http_archive( name = "build_bazel_rules_nodejs", sha256 = "36b7a8eb0854f3e5c83c9d40e3b4e7c0f996d0a6d0c9c7d9e77c8d0f3a9a8b0a", # Check latest urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.0/rules_nodejs-5.8.0.tar.gz"], ) load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies") build_bazel_rules_nodejs_dependencies()Sample Packages (Monorepo Structure)
monorepo/ ├── WORKSPACE ├── .bazelrc ├── apps/ │ ├── go-app/ BUILD + main.go │ ├── py-app/ BUILD + main.py + requirements.txt │ └── js-app/ BUILD + package.json + index.js └── libs/ └── shared/ BUILD + shared.go + shared.pyapps/go-app/BUILD:go_binary( name = "go-app", srcs = ["main.go"], importpath = "monorepo/apps/go-app", deps = ["//libs/shared:go-shared"], # Declared dep (hermetic) )apps/go-app/main.go:package main import "fmt" import "monorepo/libs/shared" func main() { fmt.Println(shared.Hello()) }libs/shared/BUILD:load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_shared", srcs = ["shared.go"], importpath = "monorepo/libs/shared", visibility = ["//apps/go-app:__pkg__"], ) # Python lib too py_library( name = "py_shared", srcs = ["shared.py"], visibility = ["//apps/py-app:__pkg__"], )apps/py-app/BUILD:py_binary( name = "py-app", srcs = ["main.py"], data = [":requirements.txt"], deps = ["//libs/shared:py_shared"], python_version = "PY3", )apps/py-app/main.py:from libs.shared import hello print(hello())libs/shared/shared.py:def hello(): return "Hello from shared Python!"apps/js-app/BUILD(similar for Node; usenodejs_binary).Step 4: Build and Verify
# Clean build (tests hermeticity) bazel clean --expunge # Build everything (sandboxed, hermetic) bazel build //... --config=remote # Run bazel run //apps/go-app:go-app bazel run //apps/py-app:py-app # Cache hit verification (2nd run should be fast/remote) time bazel build //... --config=remote # ~seconds on cache hitVerify Features:
- Sandboxing:
bazel build //... --sandbox_debug. Check logs: actions run in/tmp/bazel-sandbox-*(no host pollution). - Hermeticity: Undeclared dep? Fails with "undeclared inclusion". No external tools/pkgs unless pinned in WORKSPACE.
- Remote Cache: First build uploads artifacts. Clone repo elsewhere, build: instant if cache hits.
- Check cache:
bazel --config=remote build //... --explain=explain.logthencat explain.log.
- Check cache:
Scaling & Tips
- CI/CD: Use GitHub Actions + BuildBuddy (free 1k min/mo).
- Remote Execution: For full RBE, add
--remote_executor=grpcs://remote.buildbuddy.io+ auth. - Multi-lang: Add rules for Rust/Java/etc. via WORKSPACE.
- Debug:
--subcommands --verbose_failures. - Alternatives: Buck2 (Meta), Pants (Twitter) – similar but Bazel is most mature.
This setup gives you a production-grade monorepo build in ~15 mins. Push to GitHub, share cache URL for team. Questions? Paste errors! 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.