Sunday , November 29 2026
Automate ML testing and deployment: pytest model tests, GitHub Actions workflow, quality gates and continuous training.

CI/CD for Machine Learning with GitHub Actions

Updated: 28 November 2026Level: AdvancedReading time: about 10 minutesGitHub Actions · pytest · tested

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

tests/test_model.py
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,)
Example
pytest -q
Output
..                                                     [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

.github/workflows/ml-ci.yml
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

  1. Add a data test that fails if any feature has missing values.
  2. Add a job that runs the FastAPI app and checks /health.
  3. 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

About Machine Learning

Check Also

MLflow Tutorial: Experiment Tracking and Model Registry

Track ML experiments with MLflow: parameters, metrics, models, autologging, model registry and serving.