Sub topic 16
Running Checks Automatically
Assume you know basic Python, Git, the command line, and how to run a test locally. This chapter shows how continuous integration, or CI, runs the same checks whenever code changes.
CI is an automated job that starts after a push or pull request. It creates a clean environment, installs the project, runs checks, and reports success or failure. This catches problems before a change is merged.
What CI should check
A machine learning project should check more than whether the training script starts. Fast checks are useful on every change. Expensive training runs usually belong in a separate workflow or a scheduled job.
| Check | What it catches | Typical command |
|---|---|---|
| Tests | Broken functions and data handling | `pytest` |
| Formatting | Inconsistent source layout | `ruff format --check .` |
| Linting | Suspicious or unused code | `ruff check .` |
| Configuration | Invalid or missing settings | `python -m my_project.config_check` |
| Type checking | Incorrect value types | `mypy src` |
A CI check must fail when a problem exists. A command that prints a warning but returns exit code zero will not block a merge.
A minimal GitHub Actions workflow
Create .github/workflows/checks.yml:
name: checks
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install project
run: |
python -m pip install --upgrade pip
pip install -e ".[test]"
- name: Check formatting
run: ruff format --check .
- name: Check lint
run: ruff check .
- name: Run tests
run: pytestThe workflow runs for pushes to main and for pull requests. actions/checkout downloads the repository. actions/setup-python installs the requested Python version. The editable install, written as pip install -e, makes the source package importable without copying it.
The [test] extra assumes that pyproject.toml declares tools such as pytest and ruff in an optional dependency group. Pin their versions, or use a lock file, so a future release does not unexpectedly change the result.
Design checks for machine learning code
Use small, deterministic test data. Do not download a large dataset or train a full model for every pull request. Test preprocessing with a few rows, verify expected columns and types, and check that configuration overrides are parsed correctly.
Keep secrets out of workflow files. Store credentials in the CI provider's secret store and pass them as environment variables only to the job that needs them. Do not print those variables in logs.
The check flow
Require the workflow to pass before merging. This branch rule turns the checks into a team agreement rather than a report that people may ignore.
Exercise
Why should a pull request workflow use small deterministic fixtures instead of retraining on the full production dataset?
Show answer
Small deterministic fixtures keep feedback fast and make failures reproducible. Full training is slower, costs more, and can fail because of unrelated data or infrastructure changes.