Skip to content

tomsoir/bazel-monorepo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Bazel Multi-Language Monorepo Example

This is a unified mono repo example that demonstrates how to develop, build, test, and deploy full-stack web projects, infrastructure code, and build tools using Bazel. It is designed for scalability, fast builds, and optimized deployment.

Main Features:

  1. Repo package managers:
    • PNPM;
    • PYPI;
  2. Programming languages support:
    • TypeScript / JavaScript (Aspect's rules_ts);
      • Building:
      • Testing:
        • Tests spec files separation from the source code in Bazel (:tsc and :tsc_tests are separate);
        • Tests coverage;
        • Mocha/Chai support;
        • Vitest support;
      • Developing:
        • Run local dev server (Webpack);
        • Run local dev server (RSpack);
    • Python (Aspect's rules_py);
      • Building:
        • Build internal packages;
        • Build external packages (PYPI package manager support);
      • Testing:
        • Tests support;
        • Tests coverage;
    • GoLang;
      • Building:
        • Build internal packages;
        • Build external packages (go_dependency support);
      • Testing:
        • Tests support;
        • Tests coverage;
  3. Formatting:
    • Ruff —> Python;
    • Prettier —> JS, TS, HTML, CSS, SCSS, SQL, Markdown;
    • Yamlfmt —> Yaml;
    • Buildifier —> Starlark;
    • Gofumpt —> Go;
    • SHFMT —> shell/bash scripts;
    • Terraform —> Terraform;
  4. Linting:
    • Eslint V9 (flat config format) —> JS, JSX, TS, TSX, CJS, MJS. (commit);
    • Stylint —> CSS, SCSS (commit);
    • Flake8 —> Py (commit);
    • Shellcheck —> shell/bash scripts (commit);
  5. Development:
    • Add Remote Cache Execution support with BuildBuddy;
    • Add watcher IBazel to re-build targets on source files change (instruction);
    • Add custom Bazel rules examples:
      • Simple Bazel rule example of creating a file in bazel-bin (commit);
      • Simple Bazel rule example of using a dependency rule (commit);
      • Simple Bazel rule example of using external (Go-app) binary (commit);
      • Simple Bazel rule example of writing back to the source of the project workspace (commit);
    • Auto-generate documentation;
    • Use Gazelle to auto-update BUILD files;
    • Custom scripts:
      • New bazel command to create a new project using a template;
  6. CI/CD integration:
    • Renovate bot support to update monorepo dependencies;
    • Add dependencies review for PRs (license, Vulnerabilities, denied, etc...);
    • Use local CI cache to store Bazel artifacts;
    • Build all targets;
    • Run all tests;
    • Run format check;
    • Run lint check;
  7. Infra:
    • Docker: create image;
    • Docker: push image to registry;
    • K8s: generate manifests with concat;
    • K8s: generate manifests with helm;
    • K8s: manual manifests deploy;
    • K8s: gitops manifests deploy;
    • Terraform: run plan;
    • Terraform: update cloud manifest;

Main Commands:

Bazel:

  • $ bazel build ... — build all targets in the repo.
  • $ bazel test ... — test all (testable) targets in the repo.
    • $ bazel test --test_output=all ... - to see tests output in cli.
  • $ bazel coverage ... - generate tests coverage.
    • $ bazel coverage --combined_report=lcov --test_output=all ... - to see tests output + tests coverage files (in coverage.dat).
  • $ bazel lint ... - lint all projects.
    • $ bazel lint ... --fix - lint fix all projects.
    • $ bazel lint //experimental/service_test_ts_webpack_react_tests/... — lint individual project.

PNPM (for TS/JS):

  • $ pnpm update - update all projects in the monorepo.
  • $ pnpm install - install all npm deps for all projects in monorepo.
  • $ pnpm add — install package in some project (required $cd into it, example: $ pnpm add typescript --save-dev).
  • $ pnpm run — run script in package.json (example $ pnpm run build).

PYPI (for Python):

Manual dependencies update in monorepo (including transitive dependencies).

  • Use requirements.in file to manage (install new, update or remove old) Python dependencies in the entire monorepo.
  • $ bazel run //:generate_requirements_txt - run to update requirements.txt automatically.

Repo maintenance:

  • $ bazel fetch //... — fetches all external dependencies required for building all targets in your workspace. Note: It does this without actually building them.
  • $ bazel fetch @npm//... — update Bazel's copy of pnpm lockfile. Useful after adding new NPM packages.
  • $ bazel mod tidy — (analog of bazel sync in WORKSPACE) sync / check the status of dependencies in MODULES.
  • $ bazel run format — formats everything in the repo.
    • $ bazel run //:format — formats everything in the repo (explicit run from the root).
    • $ bazel run //:format.check — check if formatting needs to be applied.
  • $ bazel lint ... — lints all projects.
    • $ bazel lint //experimental/service_test_ts_webpack_react_tests/... — lints individual project.

Secrets:

  • BUILD_BUDDY_API_KEY - auth account key for BuildBuddy remote execution UI.
    • For CI use, it's stored in: repo > settings > secrets > actions > BUILD_BUDDY_API_KEY key.
    • Create a free personal account on BuildBuddy to get this key.

Other documents:

About

Bazel monorepo example

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •