bazel Command: Build Monorepos in CI
bazel builds and tests large multi-language codebases hermetically and cached.
Bazel is Google's hermetic, content-addressed build system, used for large polyglot monorepos. In CI its remote caching and incremental graph make builds reproducible and fast, at the cost of a steeper configuration model (BUILD files and a module/workspace).
Common flags
bazel build //...- build all targets (//path:targetfor one)bazel test //...- build and run all test targets--config=ci- apply a named config group from .bazelrc--remote_cache=URL- read/write a remote build cache--keep_going/-k- keep building after a failure--test_output=errors- print logs only for failing tests--jobs=N- limit concurrent actions
Example in CI
Run all tests with a CI config and a shared remote cache:
shell
bazel test //... --config=ci --remote_cache=\${BAZEL_CACHE_URL} --test_output=errorsCommon errors in CI
- ERROR: no such package 'X': BUILD file not found - missing or wrong package path
- ERROR: no such target '//a:b' - target name not defined in the BUILD file
- ERROR: Error loading package - Starlark/.bzl load or syntax error
- ERROR: Remote Cache: failed to download ... - cache backend connectivity issue
Key takeaways
- Use
//...to build/test everything; a remote cache makes CI fast. --config=cicentralizes CI-specific flags in .bazelrc.- Most failures are "no such package/target" from BUILD-file path mismatches.
Related guides
gradle Command: Build JVM Projects in CIgradle is the JVM build tool. Reference for build, test, --no-daemon, --build-cache, -x, --stacktrace, and th…
ninja Command: Fast Builds in CIninja is a small, fast build system. Reference for -j, -C, -f, -t, -k, -v, and the ninja CI errors you will s…
go build Command: Compile Go in CIgo build compiles Go packages. Reference for -o, -ldflags, -tags, -race, GOOS/GOARCH cross-compilation, and t…