Sub topic 11
Keeping CI Fast and Affordable
Assume you know Python, pytest, GitHub Actions, and the difference between a pull request and a scheduled job. This chapter focuses on deciding which machine checks run for each event. The goal is quick feedback for code changes without skipping checks that protect data and model quality.
Separate fast checks from heavy checks
Machine learning (ML) projects often have more expensive checks than conventional software projects. A unit test may finish in milliseconds, while training a model can require several minutes and substantial memory. Running every check on every pull request makes developers wait and increases CI spending.
Use a layered schedule:
- Run fast code, data, and small model tests on every pull request.
- Run a small pipeline smoke test on every pull request when it is cheap enough.
- Run full training, large validation sets, drift analysis, and cross-validation on demand or on a schedule.
- Run deployment checks only after the relevant quality gates pass.
The exact boundary depends on your project. A ten-minute check may be reasonable for a critical service. A two-hour training job usually belongs outside the default pull-request path.
| Check | Pull request | Push to main | Scheduled or manual |
|---|---|---|---|
| Unit and preprocessing tests | Yes | Yes | No |
| Small committed sample data | Yes | Yes | No |
| Model behavior tests | Yes | Yes | No |
| Full training run | Usually no | Sometimes | Yes |
| Drift and large-scale evaluation | No | No | Yes |
Control when jobs run
CI providers charge for runner time, and parallel jobs can consume that time quickly. Use event triggers deliberately. Pull requests need fast feedback. A nightly workflow can run checks that are too slow for every change. A manual trigger lets a developer run an expensive check before merging when needed.
GitHub Actions supports workflow_dispatch for manual runs and schedule for cron-based runs. The following workflow keeps pull requests focused on fast tests and provides a separate entry point for a longer evaluation.
name: ml-checks
on:
pull_request:
push:
branches: [main]
workflow_dispatch:
schedule:
- cron: "0 3 * * *"
jobs:
fast:
if: github.event_name != 'schedule'
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
- run: pytest tests/unit tests/data tests/model -q
full-evaluation:
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
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
- run: python scripts/train_and_evaluate.pyThe fast job runs for pull requests and pushes to main. The full-evaluation job runs only from the nightly schedule or a manual request. A scheduled workflow still needs access to the correct data and configuration. Do not silently substitute a different dataset just to make the job pass.
Reduce repeated work
Caching avoids downloading the same dependencies on every run. Pin dependencies in requirements.txt so a cached environment has predictable contents. Cache package downloads, not mutable model outputs or unversioned datasets. A stale model artifact can hide a real regression.
Other cost controls include:
- Keep pull-request datasets small, committed, and representative.
- Reuse fixtures instead of retraining a model in every test.
- Set timeouts so hung jobs stop consuming runner time.
- Avoid unnecessary matrix combinations. Test every supported Python version only when compatibility requires it.
- Cancel obsolete pull-request runs when a newer commit arrives.
- Store full logs and metrics as artifacts only for jobs that need later inspection.
Choose a cost policy
A practical policy assigns each check a maximum acceptable duration and a trigger. For example, unit tests might have a two-minute target, while nightly training might have a thirty-minute limit. Record the reason for each scheduled check. This prevents the workflow from becoming a collection of unexplained jobs.
Exercise
A full training job takes 25 minutes and costs more than the pull-request checks. When should it run?
Show answer
Run it on a schedule or through a manual workflow, while keeping deterministic unit, data, model behavior, and quality-gate tests on every pull request.