CI/CD for machine learning automatically tests your code, data and model every time you push a change, and deploys the model only when everything passes. It brings the safety of software engineering to ML.
What You Will Learn
- What CI and CD mean for ML
- Write tests for a model with pytest
- A GitHub Actions workflow that trains, tests and builds
- Quality gates before deployment
- Continuous training
Prerequisites
Docker for ML and Git basics.
CI vs CD
| Term | Means | In ML |
|---|---|---|
| Continuous Integration | Test every change automatically | Unit tests, data checks, model accuracy test |
| Continuous Delivery/Deployment | Release automatically when tests pass | Build Docker image, deploy the new model |
| Continuous Training | Retrain on new data automatically | Scheduled retraining with evaluation gates |
Step 1: Tests for the Model
import joblib
from sklearn.datasets import load_iris
def test_model_accuracy():
X, y = load_iris(return_X_y=True)
model = joblib.load("iris_model.joblib")
assert model.score(X, y) >= 0.9 # quality gate
def test_prediction_shape():
model = joblib.load("iris_model.joblib")
assert model.predict([[5.1, 3.5, 1.4, 0.2]]).shape == (1,)
pytest -q
.. [100%] 2 passed
Explanation: If a change makes accuracy drop below 0.9, the test fails and the pipeline stops before deployment. In real projects, test on a held-out set, not the training data.
Step 2: GitHub Actions Workflow
name: ml-ci
on:
push:
branches: [main]
pull_request:
jobs:
test-and-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt pytest
- run: python train.py
- run: pytest -q
- name: Build Docker image
if: github.ref == 'refs/heads/main'
run: docker build -t iris-api:${{ github.sha }} .
Explanation: On every push or pull request, GitHub starts a clean machine, installs dependencies, trains, runs the tests, and – only on the main branch – builds a Docker image tagged with the commit ID. A later step can push it to a registry and deploy it.
What to Test
| Test | Example |
|---|---|
| Code | Feature functions return the expected values |
| Data | No missing target, columns present, values in range |
| Model | Accuracy ≥ baseline; not worse than the current production model |
| API | /predict returns 200 and valid JSON |
| Fairness / bias | Similar error rates across groups |
Continuous Training
Schedule the workflow (on: schedule: - cron: "0 2 * * 1") to retrain weekly on fresh data. Compare the new model with the current one and promote it in MLflow only if it wins.
Common Errors
| Problem | Cause and fix |
|---|---|
| Tests pass locally, fail in CI | Missing package in requirements.txt or a file not committed. |
| Flaky accuracy test | Fix random seeds and use a threshold with some margin. |
| Secrets in logs | Store keys in GitHub Secrets and use secrets.NAME. |
Series
MLOps series: ML Deployment · Flask & FastAPI · Docker · Kubernetes · MLflow · CI/CD for ML · AWS for ML · Google Cloud AI
Interview Questions
Q: What is a quality gate?
A check (such as minimum accuracy) that must pass before a model can be deployed.
Q: Continuous training vs continuous deployment?
CT retrains the model automatically; CD releases code or models automatically after tests.
Practice Exercises
- Add a data test that fails if any feature has missing values.
- Add a job that runs the FastAPI app and checks /health.
- Schedule weekly retraining.
Summary
CI/CD for ML tests code, data and model on every change, builds deployable images automatically and can retrain on schedule – with quality gates protecting production.
Next: AWS for ML · Google Cloud AI
Machine Learning Tutorials, Courses and Certifications