Skip to content

b95702041/bazel-python-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Bazel Python Demo

A simple Python project demonstrating Bazel build system fundamentals with Jenkins CI/CD integration. Perfect for learning how Bazel works with Python code, dependencies, incremental builds, and automated pipelines.

🎯 What This Project Demonstrates

  • Basic Bazel concepts: WORKSPACE, BUILD files, targets
  • Python with Bazel: py_library and py_binary rules
  • Dependency management: How modules depend on each other
  • Incremental builds: Fast rebuilds when nothing changes
  • Cross-platform builds: Works on Windows, macOS, and Linux
  • CI/CD Integration: Automated builds with Jenkins
  • Containerized CI: Jenkins running in Docker with automatic Bazel installation

πŸ“ Project Structure

bazel-python-demo/
β”œβ”€β”€ WORKSPACE           # Bazel project root (empty file)
β”œβ”€β”€ MODULE.bazel        # Bazel module configuration
β”œβ”€β”€ BUILD.bazel         # Build rules and targets
β”œβ”€β”€ hello.py            # Main executable program
β”œβ”€β”€ math_utils.py       # Reusable Python library
β”œβ”€β”€ Jenkinsfile.bazel   # Jenkins CI/CD pipeline
β”œβ”€β”€ .gitignore          # Ignore Bazel output directories
└── README.md           # This documentation

πŸš€ Quick Start

Prerequisites

Windows:

# Install Bazel using Chocolatey (run PowerShell as Administrator)
choco install bazel

macOS:

# Install Bazel using Homebrew
brew install bazel

Linux (Ubuntu/Debian):

# Install Bazel
sudo apt update
sudo apt install bazel

Run the Demo

# Clone the repository
git clone https://github.com/b95702041/bazel-python-demo.git
cd bazel-python-demo

# Build and run the program
bazel run //:hello

Expected output:

Hello from Python with Bazel!
2 + 3 = 5
4 * 5 = 20

πŸ“š Understanding the Code

math_utils.py - Python Library

def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

Simple math functions that can be reused by other modules.

hello.py - Main Program

from math_utils import add, multiply

def main():
    print("Hello from Python with Bazel!")
    print(f"2 + 3 = {add(2, 3)}")
    print(f"4 * 5 = {multiply(4, 5)}")

if __name__ == "__main__":
    main()

Imports and uses functions from math_utils.py.

BUILD.bazel - Build Rules

# Create a reusable Python library
py_library(
    name = "math_utils",
    srcs = ["math_utils.py"],
)

# Create an executable Python program
py_binary(
    name = "hello",
    srcs = ["hello.py"],
    deps = [":math_utils"],  # Depends on the math_utils library
)

Key concepts:

  • py_library: Creates a reusable Python module
  • py_binary: Creates an executable Python program
  • deps: Declares dependencies between targets
  • name: The target name (used with bazel run //:hello)
  • srcs: Source files for this target

πŸ”„ CI/CD Pipeline

This project includes a complete Jenkins CI/CD pipeline that demonstrates automated Bazel builds.

Pipeline Stages

  1. πŸ”§ Install Bazelisk - Automatically installs Bazel using Bazelisk
  2. πŸ“₯ Checkout - Gets latest code from GitHub
  3. ℹ️ Bazel Info - Shows Bazel version and configuration
  4. 🧹 Clean Build - Cleans previous build artifacts
  5. πŸ—οΈ Build All Targets - Builds all Bazel targets
  6. πŸ§ͺ Run Tests - Executes all Bazel tests
  7. πŸš€ Run Application - Executes the Python application
  8. πŸ” Query Targets - Shows all available Bazel targets

Jenkins Setup

Prerequisites:

  • Docker Desktop installed
  • Jenkins running in Docker container

Run Jenkins in Docker:

docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts

Configure Jenkins Pipeline:

  1. Create new Pipeline job
  2. Set Repository URL: https://github.com/b95702041/bazel-python-demo.git
  3. Set Script Path: Jenkinsfile.bazel
  4. Run the pipeline

Pipeline Features

  • βœ… Automatic Bazel Installation - Uses Bazelisk for easy setup
  • βœ… No Root Privileges Required - Installs to user directory
  • βœ… Cross-Platform Compatible - Works in Docker containers
  • βœ… Complete Build Lifecycle - Clean, build, test, run
  • βœ… Error Handling - Robust cleanup and error reporting

⚑ Bazel Performance

First run (cold build):

INFO: Elapsed time: 66.928s, Critical Path: 7.41s

Second run (cached build):

INFO: Elapsed time: 2.333s, Critical Path: 0.09s

This demonstrates Bazel's incremental build capability - it only rebuilds what has changed.

πŸ› οΈ Useful Bazel Commands

# Build everything in the project
bazel build //...

# Run a specific target
bazel run //:hello

# See the dependency graph
bazel query //...

# Get detailed build information
bazel query --output=build //:hello

# Clean build artifacts
bazel clean

# Check Bazel version
bazel --version

πŸ” Exploring Further

Add a Test

Create math_utils_test.py:

import unittest
from math_utils import add, multiply

class TestMathUtils(unittest.TestCase):
    def test_add(self):
        self.assertEqual(add(2, 3), 5)
    
    def test_multiply(self):
        self.assertEqual(multiply(4, 5), 20)

if __name__ == '__main__':
    unittest.main()

Add to BUILD.bazel:

py_test(
    name = "math_utils_test",
    srcs = ["math_utils_test.py"],
    deps = [":math_utils"],
)

Run tests:

bazel test //:math_utils_test

Understanding the MODULE.bazel File

The MODULE.bazel file (Bazel 6.0+) defines module dependencies and is the modern replacement for WORKSPACE. It provides:

  • Module metadata
  • Dependency declarations
  • Version compatibility

πŸ—οΈ How Bazel Works

  1. Analysis Phase: Bazel reads BUILD files and creates a dependency graph
  2. Execution Phase: Bazel executes only the actions needed to build requested targets
  3. Caching: Results are cached for fast incremental builds
  4. Parallelization: Independent targets build in parallel

πŸ€” Why Use Bazel?

  • Fast builds: Incremental and parallel builds
  • Reproducible: Same input always produces same output
  • Multi-language: Supports many programming languages
  • Scalable: Works for small projects to massive codebases
  • Remote execution: Can distribute builds across machines
  • CI/CD Ready: Perfect for automated build pipelines

πŸ”— Related Projects

This project is part of a multi-repository CI/CD demonstration:

Together, they demonstrate:

  • Multi-language CI/CD pipelines
  • Different build systems (npm vs Bazel)
  • Jenkins integration with Docker
  • Polyglot development workflows

πŸ“– Learn More

πŸ› Troubleshooting

Common issues:

  1. "bazel: command not found"

    • Make sure Bazel is installed and in your PATH
    • In CI/CD, the pipeline automatically installs Bazelisk
  2. "No such package" errors

    • Check that your BUILD.bazel file is in the right location
    • Verify target names match what you're trying to build
  3. Python import errors

    • Make sure dependencies are declared in BUILD.bazel
    • Check that file names match exactly
  4. Permission errors on Windows

    • Run PowerShell as Administrator when installing Bazel
  5. Jenkins pipeline failures

    • Check that Bazelisk downloaded successfully
    • Verify PATH is set correctly in pipeline stages
    • Look for network connectivity issues

βœ… Pipeline Status

Latest Build: βœ… SUCCESS

βœ… Bazelisk: v1.26.0
βœ… Bazel: 8.3.1
βœ… Targets: 2 built successfully
βœ… Tests: All passed
βœ… Application: Executed successfully

🀝 Contributing

Feel free to:

  • Add more math functions to math_utils.py
  • Create additional Python modules
  • Add tests for the functions
  • Experiment with different Bazel rules
  • Improve the Jenkins pipeline
  • Add more CI/CD integrations

πŸ“ License

This project is for educational purposes. Feel free to use and modify as needed.


Next steps: Try adding more Python files, creating tests, exploring Bazel's query capabilities, or integrating with other CI/CD tools!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published