How to simulate a Jenkins pipeline in Python

Simulate a Jenkins-style pipeline in Python by running sequential stages and checking aggregate success.

Easy Python 3.6+ Aug 9, 2026 Production deployment patterns 13 views 0 copies

Python code

31 lines
Python 3.6+
def run_stage(name, duration, fn):
    print(f"[Pipeline] Running stage: {name}")
    result = fn()
    print(f"[Pipeline] Stage '{name}' completed in {duration}s -> {result}")
    return result

def build_project():
    print("  compiling source...")
    return "BUILD_OK"

def run_tests():
    print("  executing unit tests...")
    return "TESTS_PASSED"

def deploy():
    print("  deploying to staging...")
    return "DEPLOYED"

if __name__ == "__main__":
    stages = [
        ("Build", 10, build_project),
        ("Test", 20, run_tests),
        ("Deploy", 5, deploy),
    ]
    print("=== Jenkins Pipeline Simulation ===")
    all_results = [run_stage(name, dur, fn) for name, dur, fn in stages]
    if all(r == "OK" for r in all_results):
        print("Pipeline succeeded")
    else:
        print("Pipeline finished with mixed results")
    print("Final status:", "SUCCESS" if all(r.endswith("OK") or r.endswith("PASSED") or r.endswith("DEPLOYED") for r in all_results) else "FAILURE")

Output

stdout
=== Jenkins Pipeline Simulation ===
[Pipeline] Running stage: Build
  compiling source...
[Pipeline] Stage 'Build' completed in 10s -> BUILD_OK
[Pipeline] Running stage: Test
  executing unit tests...
[Pipeline] Stage 'Test' completed in 20s -> TESTS_PASSED
[Pipeline] Running stage: Deploy
  deploying to staging...
[Pipeline] Stage 'Deploy' completed in 5s -> DEPLOYED
Pipeline finished with mixed results
Final status: SUCCESS

How it works

The run_stage wrapper logs the stage name, runs the provided callable, and logs the result. Using a list of tuples holds each stage's name, duration, and function. The list comprehension executes all stages and collects their results. The first check uses all(r == "OK") to test exact equality, which fails because results are BUILD_OK, TESTS_PASSED, and DEPLOYED. A more realistic check inspects suffix patterns, and the final status reflects SUCCESS since every result ends with a recognized keyword. This mirrors how CI systems track per-stage outcomes and derive an overall build status.

Common mistakes

  • Mixing up exact equality (`== "OK"`) with pattern-based checks when stage results differ
  • Assuming stages run in parallel — Jenkins pipelines may, but this code is sequential
  • Forgetting to pass each stage function as a callable object, not the result of calling it

Variations

  1. Use a dataclass to represent a stage with name, duration, and function attributes
  2. Wrap the stage execution in try/except to handle failed stages and stop the pipeline early

Real-world use cases

  • Prototyping a CI/CD pipeline locally without Jenkins installed to verify stage ordering.
  • Testing failure-handling logic by mocking stages with synthetic results before wiring real commands.
  • Documenting or onboarding new engineers on how a build, test, and deploy sequence should behave.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Production deployment patterns

Related tutorials and quizzes for this topic.