Bazel Multi-Language TutorialΒΆ
π― View Live Dashboard
The Economic Indicators Dashboard is deployed live on GitHub Pages:
The dashboard automatically updates daily with fresh economic data from FRED API.
Run Locally
Want to run it yourself?
- Clone the repo and set your FRED API key
- Run
./run_all.sh - Start the UI:
cd web_ui && npm run dev - Open http://localhost:3000
A comprehensive, hands-on guide to building polyglot applications with Bazel β Google's fast, reproducible, and scalable build system.
π― What You'll LearnΒΆ
This repository demonstrates real-world Bazel usage across multiple languages:
- β Build Go, Python, and React services in one unified repository
- β Incremental compilation and caching for lightning-fast builds
- β Hermetic testing without network dependencies
- β Modern toolchain integration (rules_go, rules_python, npm/vite)
- β CI/CD with GitHub Actions and Bazel caching
- β Real-world architecture patterns for data fetching, APIs, and UIs
π Quick StartΒΆ
# Install Bazel
brew install bazel # macOS
# or download from https://bazel.build/install
# Clone the repository
git clone https://github.com/amulya-labs/bazel-tutorial.git
cd bazel-tutorial
# Build everything
bazel build //...
# Run tests
bazel test //...
π What's InsideΒΆ
Basic Tutorial ServicesΒΆ
Simple services to learn Bazel fundamentals:
| Service | Language | Description | Command |
|---|---|---|---|
py_service |
Python (FastAPI) | "Hello from Python" web service | bazel run //py_service:server |
go_service |
Go | "Hello from Go" web service | bazel run //go_service:server |
π Economic Indicators DashboardΒΆ
A full-stack polyglot application demonstrating real-world Bazel usage:
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Go Fetch β writes β SQLite DB β reads β Python API β
β Service βββββββββββ (econ.db) βββββββββββ (FastAPI) β
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β
β HTTP
β
βββββββββββββββ
β React UI β
β (Vite) β
βββββββββββββββ
Features: - Go service fetches economic data from FRED API - Python FastAPI serves REST endpoints with computed metrics - React frontend with interactive charts and responsive design - Tracks 6 core economic indicators (CPI, Unemployment, Fed Funds, etc.)
Quick Start:
# Get a free FRED API key from https://fred.stlouisfed.org
export FRED_API_KEY=your_key_here
# Run everything
./run_all.sh
# In another terminal, start the UI
cd web_ui && npm install && npm run dev
Full Dashboard Documentation β
ποΈ Repository StructureΒΆ
bazel-tutorial/
βββ MODULE.bazel # Bazel dependencies (Bzlmod)
βββ .bazelrc # Bazel configuration
βββ .bazelversion # Pin Bazel version
β
βββ go_service/ # Basic Go service tutorial
βββ py_service/ # Basic Python service tutorial
β
βββ go_fetch/ # Economic data fetcher (Go)
βββ py_api/ # REST API server (Python)
βββ web_ui/ # Dashboard UI (React + TypeScript)
β
βββ docs/ # Documentation
βββ tests/ # Integration tests
β
βββ .github/workflows/ # CI/CD pipelines
π§± Key Bazel ConceptsΒΆ
MODULE.bazel (Bzlmod)ΒΆ
Modern external dependency management:
module(
name = "bazel_multilang_tutorial",
version = "1.0.0",
)
# Python rules
bazel_dep(name = "rules_python", version = "0.31.0")
# Go rules
bazel_dep(name = "rules_go", version = "0.46.0")
Learn more about Bazel concepts β
BUILD.bazel FilesΒΆ
Each component has a BUILD.bazel file defining buildable targets:
# Python binary
py_binary(
name = "server",
srcs = ["main.py"],
deps = ["//py_service:requirements"],
)
# Go binary
go_binary(
name = "server",
srcs = ["main.go"],
)
Targets and LabelsΒΆ
//go_service:server- The "server" target in the "go_service" package//py_api:server- Python API server binary//...- All targets in all packages (recursive)
Deep dive into dependencies β
π§ͺ TestingΒΆ
All tests use fixtures and mocks - no network access required!
# Run all tests
bazel test //...
# Run specific tests
bazel test //go_fetch:fred_test
bazel test //py_api:api_test
# View test output
bazel test //... --test_output=all
π Incremental BuildsΒΆ
Bazel only rebuilds what changed:
# First build (downloads deps, compiles everything)
bazel build //...
# Edit Python code - only Python targets rebuild!
echo "# comment" >> py_service/main.py
bazel build //...
# Edit Go code - only Go targets rebuild!
echo "// comment" >> go_service/main.go
bazel build //...
βοΈ CI/CD IntegrationΒΆ
The repository includes GitHub Actions workflows demonstrating:
- Bazel build and test automation
- Dependency caching for fast CI runs
- Multi-language build verification
Bazel's incremental caching allows the same artifacts to be reused between CI runs.
π§ Why Bazel for Polyglot Repos?ΒΆ
Benefits Demonstrated in This ProjectΒΆ
- Single Build System
- One tool for Go, Python, and JavaScript
-
Consistent commands across languages
-
Incremental Builds
- Only rebuild what changed
-
Shared cache across projects
-
Hermetic Testing
- Tests don't depend on network or system state
-
Use mocks and fixtures
-
Explicit Dependencies
- Clear dependency graph
-
No hidden dependencies
-
Scalability
- Works for small projects (this) and massive monorepos (Google)
- Parallel builds and tests
π DocumentationΒΆ
Explore comprehensive guides:
- Quick Start Guide - Get up and running quickly
- Basic Tutorial - Learn with simple services
- Economic Dashboard - Full-stack application example
- Bazel Concepts - Deep dive into Bazel fundamentals
- Dependencies - Managing Python, Go, and npm dependencies
- Query Guide - Explore the build graph with
bazel query - Testing Guide - Comprehensive testing strategies
- Contributing - Development guidelines
π§ TroubleshootingΒΆ
Common IssuesΒΆ
"command not found: bazel" - Install Bazel: https://bazel.build/install - Or use Bazelisk (recommended): https://github.com/bazelbuild/bazelisk
"Failed to fetch external repository"
- Check your internet connection
- Run bazel sync to re-fetch dependencies
- Check MODULE.bazel for typos
"Builds are slow" - First build downloads all dependencies (slow) - Subsequent builds use cache (fast!) - Enable remote caching for team collaboration
"Database not found" (Economic Dashboard)
- Run the data fetcher first: bazel run //go_fetch:refresh
- Make sure you set FRED_API_KEY environment variable
Validation ScriptΒΆ
To verify your setup is working correctly:
This script will: - Check if Bazel is installed - Validate repository structure - Build all targets - Run all tests
π€ ContributingΒΆ
Contributions are welcome! See CONTRIBUTING.md for guidelines on: - Adding new services - Improving documentation - Adding tests - Submitting pull requests
π Further ReadingΒΆ
Official DocumentationΒΆ
- Bazel Basics
- Bzlmod Guide - The new MODULE.bazel system
- rules_python
- rules_go
- Bazel Query Guide
- Bazel Remote Caching
Economic DataΒΆ
π LicenseΒΆ
MIT License - See LICENSE file for details
Happy Building! π
For questions or issues, please open a GitHub issue.