Skip to content

Bazel Multi-Language TutorialΒΆ

🎯 View Live Dashboard

The Economic Indicators Dashboard is deployed live on GitHub Pages:

πŸš€ Open Live Dashboard β†’

The dashboard automatically updates daily with fresh economic data from FRED API.

Run Locally

Want to run it yourself?

  1. Clone the repo and set your FRED API key
  2. Run ./run_all.sh
  3. Start the UI: cd web_ui && npm run dev
  4. Open http://localhost:3000

πŸ“– Full Setup Instructions β†’

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 //...

Full Quick Start Guide β†’

πŸ“š 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

Learn More β†’

πŸ“Š 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

Complete testing guide β†’

πŸ”„ 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
- name: Build and Test
  run: |
    bazel build //...
    bazel test //...

Bazel's incremental caching allows the same artifacts to be reused between CI runs.

🧠 Why Bazel for Polyglot Repos?¢

Benefits Demonstrated in This ProjectΒΆ

  1. Single Build System
  2. One tool for Go, Python, and JavaScript
  3. Consistent commands across languages

  4. Incremental Builds

  5. Only rebuild what changed
  6. Shared cache across projects

  7. Hermetic Testing

  8. Tests don't depend on network or system state
  9. Use mocks and fixtures

  10. Explicit Dependencies

  11. Clear dependency graph
  12. No hidden dependencies

  13. Scalability

  14. Works for small projects (this) and massive monorepos (Google)
  15. Parallel builds and tests

πŸ“– DocumentationΒΆ

Explore comprehensive guides:

πŸ”§ 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:

./validate.sh

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ΒΆ

Economic DataΒΆ

πŸ“„ LicenseΒΆ

MIT License - See LICENSE file for details


Happy Building! πŸš€

For questions or issues, please open a GitHub issue.