Bazel Build Setup
Overview
Bazel is used as a unified build and test runner across services. Each service delegates its actual build/test tooling (Maven, npm, etc.) to its native wrapper, while Bazel provides caching and a consistent interface.
Workspace Structure
AutoBuild/
├── .bazelrc
├── MODULE.bazel
├── pipeline/
│ ├── BUILD.bazel
│ ├── run_tests.sh
│ └── run_generate_openapi.sh
└── pipeline-ui/
├── BUILD.bazel
└── run_tests.sh
Prerequisites
-
Bazel installed
-
WORKSPACE_DIRexported in your shell profile pointing to the repo root:export WORKSPACE_DIR=/path/to/AutoBuildThis is required because Bazel runs tests in a sandbox and needs to know the real workspace path to invoke native build tools (mvnw, npm, etc.).
Running Tests
# Run pipeline (Java/Maven) tests
bazel test //pipeline:test
# Run pipeline-ui (Node/npm) tests
bazel test //pipeline-ui:unit_test
The --test_env=WORKSPACE_DIR flag is automatically applied to all bazel test
invocations via .bazelrc, so no extra flags are needed as long as WORKSPACE_DIR
is set in your shell.
Generating OpenAPI Specs
The pipeline service OpenAPI spec can be generated by starting the app and scraping
its /v3/api-docs.yaml endpoint:
bazel build --config=openapi //pipeline:generate-openapi
The generated spec is written to apis/openapi-spec/pipeline-service.yaml. The
--config=openapi flag is required — it forwards PATH and JAVA_HOME into the
build action so Maven can be found. Without it, the build will fail with
mvn: command not found.
Commit the updated spec after running this if any API changes were made.
Viewing Test Output
By default Bazel only prints output on failure. Use --test_output to control this:
| Flag | Behaviour |
|---|---|
--test_output=errors | Print output only on failure (default) |
--test_output=all | Always print full output, even on pass |
--test_output=streamed | Stream output live as the test runs (disables caching) |
--test_output=streamed is the most useful during development — you see Maven/npm output in real time:
bazel test //pipeline:test --test_output=streamed
To read the log from a previous run:
cat $(bazel info bazel-testlogs)/pipeline/test/test.log
Caching
Bazel caches test results based on input file changes. If no source files or pom.xml
have changed since the last run, the result is served from cache:
//pipeline:test (cached) PASSED in 13.5s
To force a re-run regardless of cache:
bazel test //pipeline:test --cache_test_results=no
Adding Tests for a New Service
-
Create a
run_tests.shin the service directory:#!/usr/bin/env bashset -euo pipefailexport HOME=$(echo ~)cd "${WORKSPACE_DIR:?WORKSPACE_DIR must be set}/<service-dir>"# run your tests, e.g. npm test or ./mvnw test -
Make it executable:
chmod +x run_tests.sh -
Add a
BUILD.bazelin the service directory:load("@rules_shell//shell:sh_test.bzl", "sh_test")sh_test(name = "test",srcs = ["run_tests.sh"],data = glob(["src/**", "pom.xml"], exclude = ["target/**"]),tags = ["manual", "local"],)
MODULE.bazel Dependencies
bazel_dep(name = "rules_java", version = "9.9.0")
bazel_dep(name = "rules_shell", version = "0.6.1")