Train and gate a model in a pipeline
In this task you'll train and test the model automatically on every pull request, and block the merge if it fails the gate.
The training scripts are code, so they belong in the same pipeline as other code: on every pull request, install the pinned packages, build the data, train, and run the gate. A change that makes the model worse fails the check, and branch protection blocks the merge.
Upload the scores and the model as artifacts so reviewers can see what changed. Keep heavy training (big data, GPUs) out of pull request checks: use a small sample there and run the full training on a schedule or on merge.
Example. Ironbridge Payments added a pull request check to its risk model's repository. A refactor that accidentally dropped a feature passed every unit test but failed the AUC gate, and was fixed before it reached the registry.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.
Where to look
- Repository
- .github/workflows/train.yml
- Actions tab
Steps
- In a public test repository, add make_data.py, train.py and a
requirements.txtlisting mlflow, scikit-learn and pandas (pin the versions you used: runpip freezeto see them). - Add
.github/workflows/train.ymlfrom Commands. - Open a pull request. The train job installs, builds the data, trains with
--min-auc 0.75and uploads mlflow.db as an artifact. - Change the gate to 0.95 in a second commit and push: the check fails and the pull request shows a red cross.Turn on branch protection to make that block the merge.
- Put the gate back to 0.75.
Commands
# .github/workflows/train.yml
name: train
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
train:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with: { python-version: '3.12', cache: pip }
- run: pip install -r requirements.txt
- run: python make_data.py
- run: python train.py --min-auc 0.75
- uses: actions/upload-artifact@v5
if: always()
with:
name: mlflow-run
path: mlflow.db
Watch out for
- Cost
- Free in a public repository. Keep the data small so runs take minutes, not hours.
- Security
- Never put real customer data in a public repository; this track's data is made up for that reason.
- Common mistakes
- Training the full model on every push, and unpinned packages that change results between runs.
Clean up
Delete the test repository when you've finished: gh repo delete OWNER/REPO --yes.
Words to know
- Pull request check
- A pipeline that must pass before a change can be merged.
- Pinned packages
- Exact versions written down, so every run installs the same ones.
- Artifact
- A file a pipeline run keeps, such as metrics or a model.
- Branch protection
- Repository rules such as requiring checks to pass.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On