Sub topic 10
Putting It Together in a CI Workflow
This chapter assumes that you know Python and pytest, use Git pull requests, and have basic experience with YAML-based CI services such as GitHub Actions. The goal is to connect fast code and data checks with slower model and pipeline checks.
Choose checks by speed and risk
Machine learning (ML) continuous integration (CI) must check more than source code. A pull request can pass ordinary unit tests while changing a feature definition, accepting invalid data, or producing a weaker model. Organize checks into layers so that fast feedback runs often and expensive checks run deliberately.
| Check layer | Typical contents | Pull request policy |
|---|---|---|
| Fast | Unit tests, schema checks, small fixtures | Run on every pull request |
| Medium | Model behavior tests, seeded training | Run on pull requests when practical |
| Heavy | Full training, drift analysis, pipeline smoke tests | Run on main, schedule, or manual request |
Keep test data small and committed when possible. A test should not download a changing production dataset unless the test explicitly records the dataset version.
Use a workflow with fast and heavy jobs
The following workflow runs fast checks for every pull request. It runs model and pipeline checks for pushes to main, on a weekly schedule, or when someone starts it manually. The workflow_dispatch trigger makes an expensive run available without creating a code change.
name: ml-ci
on:
pull_request:
push:
branches: [main]
schedule:
- cron: "0 3 * * 1"
workflow_dispatch:
jobs:
fast:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
- run: pip install -r requirements.txt
- name: Unit and data tests
run: pytest tests/unit tests/data -q
heavy:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
- run: pip install -r requirements.txt
- name: Model tests
run: pytest tests/model -q
- name: Pipeline smoke test
run: pytest tests/pipeline -qThe workflow installs the same dependency file for every job. Pin important versions in requirements.txt, such as pandas==2.2.3 and scikit-learn==1.5.2. A dependency upgrade should be a visible change that can be reviewed.
Make failures useful
Tests should fail with a message that identifies the broken contract. For example, a quality-gate function can return readable failures, while a CI step turns any failure into a nonzero exit status.
def enforce_quality_gate(candidate: dict, baseline: dict) -> None:
failures = []
accuracy = candidate["accuracy"]
if accuracy < 0.85:
failures.append(f"accuracy {accuracy:.3f} is below 0.850")
if baseline["accuracy"] - accuracy > 0.02:
failures.append(
f"accuracy dropped from {baseline['accuracy']:.3f} "
f"to {accuracy:.3f}"
)
if failures:
raise AssertionError("; ".join(failures))Store test reports, metric files, and model metadata as CI artifacts when a heavy job runs. Record the commit, dependency lockfile, dataset identifier, random seed, and model configuration with each result. A metric without these values is difficult to reproduce or compare.
Connect CI to delivery
CI should produce evidence for a later delivery or retraining process, not deploy every model automatically. A common handoff is:
Protect the main branch with the fast job as a required status check. Require approval for heavy jobs that can consume substantial compute or access sensitive data. Keep deployment credentials out of ordinary pull-request jobs.
Exercise
A pull request changes feature preprocessing. Which checks should run immediately, and which check can wait for main or a scheduled run?
Show answer
Run the preprocessing unit tests and data validation checks immediately. Run full training, drift analysis, and the complete pipeline smoke test on main, on a schedule, or manually if those checks are expensive.