Sub topic 10
Running the Pipeline from the CLI, CI, or an Orchestrator
This chapter assumes you know Python functions and modules, basic Git and command-line use, and the usual train, validate, and test workflow. It also assumes that an earlier chapter defined TrainConfig and a run_pipeline function that returns a trained model and its metrics.
A pipeline becomes useful when another system can start it without opening a notebook. The same code should run on a developer's machine, in continuous integration (CI), or inside a workflow orchestrator. Each environment changes how the run is triggered and monitored, but the pipeline logic should remain the same.
Expose the pipeline through a command-line interface
A command-line interface (CLI) is a program that accepts options from a shell. The CLI should translate those options into configuration, call the pipeline, print useful results, and return a meaningful exit status.
# cli.py
import argparse
import json
from pipeline import TrainConfig, run_pipeline
def main() -> int:
parser = argparse.ArgumentParser(description="Run the training pipeline")
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--test-size", type=float, default=0.2)
parser.add_argument("--n-estimators", type=int, default=100)
parser.add_argument("--min-accuracy", type=float, default=0.8)
args = parser.parse_args()
config = TrainConfig(
seed=args.seed,
test_size=args.test_size,
n_estimators=args.n_estimators,
min_accuracy=args.min_accuracy,
)
try:
_, metrics = run_pipeline(config)
except ValueError as error:
print(f"PIPELINE_FAILED: {error}")
return 1
print(json.dumps(metrics, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())Run it locally:
python cli.py --seed 42 --n-estimators 200 --min-accuracy 0.80The raise SystemExit(main()) line converts the returned integer into the process exit code. A zero means success. A nonzero value means failure. CI systems and orchestrators use this convention to decide whether a task passed.
Keep options that affect the run visible. Avoid hidden values from a notebook cell or a developer's local environment. For larger projects, load a versioned configuration file and allow carefully chosen command-line overrides.
Run the same command in CI
CI runs checks after events such as a pull request or a merge. A training pipeline can run as a smoke test on every change, or as a longer training job when data or model code changes.
For example, a GitHub Actions workflow can install dependencies and invoke the same CLI:
name: training-pipeline
on:
pull_request:
push:
branches: [main]
jobs:
train:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: python -m pip install -r requirements.txt
- name: Run training pipeline
run: python cli.py --min-accuracy 0.80The workflow does not reimplement loading, splitting, training, or evaluation. It only prepares the environment and starts the pipeline. If the evaluation gate raises an error, the CLI exits with status 1 and the CI job fails.
CI is a good place to verify that the pipeline still runs from a clean environment. It is not always the right place for expensive production training. Long jobs may need a separate workflow, a larger runner, or an orchestrator with queueing and resource controls.
Hand the pipeline to an orchestrator
An orchestrator schedules tasks, manages dependencies, records task state, and often retries failed tasks. Examples include Airflow, Prefect, Dagster, and managed cloud workflow services.
The orchestrator should call stable pipeline stages rather than contain the model's business logic. A typical flow looks like this:
An orchestrator is useful when you need scheduled runs, dependency-aware execution, resource limits, logs, retries, or connections to a model registry. It does not make an unsafe pipeline safe automatically. The pipeline still needs versioned inputs, explicit outputs, evaluation gates, and reliable artifact storage.
Choose the execution layer
| Execution layer | Best use | Main responsibility |
|---|---|---|
| CLI | Local development and debugging | Start one run with explicit options |
| CI | Tests and controlled repository events | Rebuild the environment and reject broken changes |
| Orchestrator | Scheduled or multi-stage production workflows | Schedule tasks, track state, retry, and manage dependencies |
Use the CLI as the smallest execution unit. Put that CLI in CI before adding an orchestrator. This sequence keeps failures easy to reproduce. When an orchestrator retries a task, it should execute the same deterministic stage with the same input versions and configuration.
Make failures visible and safe to retry
A task should fail loudly when required input is missing or an evaluation gate fails. Do not catch every exception and return success. That causes an orchestrator to mark a broken run as complete.
Retries are appropriate for temporary failures such as network timeouts or an unavailable worker. They are not a solution for deterministic errors such as invalid data or accuracy below the threshold. Configure limited retries and preserve logs from each attempt.
A production workflow should also store the configuration fingerprint, input-data version, source commit, metrics, and model artifact. These records let you distinguish a code failure from a data change or an expected evaluation failure.
Exercise
A CI job runs python cli.py, but the model scores below the minimum accuracy. What should the job report, and why?
Show answer
The CLI should return a nonzero exit status after the evaluation gate fails. CI should mark the job as failed and prevent model registration or deployment, while preserving the metrics and logs for diagnosis.