使用 GitHub Actions 实现自动重新训练管道
您将编写 GitHub Actions 工作流,使其按数据计划触发训练、评估新模型,并仅在新模型优于生产基线时将其晋级。
使用 GitHub Actions 实现自动重新训练管道 是 CoddyKit 上的免费 Machine Learning Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Machine Learning Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Machine Learning Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Automate Model Retraining?
Models trained on historical data degrade over time as real-world patterns shift. Manually retraining requires a data scientist to remember to do it, run scripts, evaluate results, and update deployment — a process prone to delays and human error. Automated retraining pipelines run on a schedule or when triggered by data arrival, automatically train a new model, evaluate it against the current champion, and promote it only if performance improves — with zero manual intervention.
GitHub Actions: Workflows and Triggers
GitHub Actions is a CI/CD platform built into GitHub. Workflows are defined in YAML files under .github/workflows/ and run in response to triggers: code pushes, pull requests, manual dispatches, or cron schedules. Each workflow consists of jobs (groups of steps) that run on hosted or self-hosted runners. For ML pipelines, a scheduled cron trigger on a cloud GPU runner is the most common pattern.
# .github/workflows/retrain.yml (YAML structure shown)
# name: Model Retraining Pipeline
#
# on:
# schedule:
# - cron: '0 2 * * 1' # every Monday at 2 AM UTC
# workflow_dispatch: # also allow manual trigger
#
# jobs:
# retrain:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Set up Python
# uses: actions/setup-python@v5
# with:
# python-version: '3.11'
print('Workflow YAML structure shown above.')Cron Syntax for Scheduling
GitHub Actions cron expressions follow standard Unix cron format: minute hour day month weekday. Common ML retraining schedules include weekly retraining (when new weekly data batches are available) and nightly retraining for fast-moving domains. Use workflow_dispatch alongside cron so data scientists can also trigger retraining manually when they push a new training script or detect drift.
# Common cron schedule examples for ML pipelines
# '0 2 * * 1' -- Every Monday at 02:00 UTC (weekly)
# '0 3 * * *' -- Every day at 03:00 UTC (nightly)
# '0 */6 * * *' -- Every 6 hours
# '0 2 1 * *' -- First day of every month at 02:00
# All times are UTC in GitHub Actions
# Use https://crontab.guru to verify cron expressions
# Example in YAML:
# on:
# schedule:
# - cron: '0 3 * * *' # nightly at 3 AM UTC
# workflow_dispatch:
print('Cron schedule examples shown above.')Checkout, Setup Python, Install Deps
The first steps in every ML workflow checkout the repository code, set up the required Python version, and install dependencies from requirements.txt. Use dependency caching with actions/cache to avoid reinstalling packages on every run — this can reduce pipeline duration from 10 minutes to under 2 minutes by reusing the cached pip packages when requirements.txt has not changed.
# .github/workflows/retrain.yml -- steps section
# steps:
# - uses: actions/checkout@v4
#
# - name: Set up Python 3.11
# uses: actions/setup-python@v5
# with:
# python-version: '3.11'
#
# - name: Cache pip packages
# uses: actions/cache@v4
# with:
# path: ~/.cache/pip
# key: pip-${{ hashFiles('requirements.txt') }}
#
# - name: Install dependencies
# run: pip install -r requirements.txt
print('Workflow steps shown above.')Secrets Management in GitHub Actions
API keys, database passwords, and cloud credentials must never be hard-coded in workflow files. Store them as GitHub Secrets (Settings → Secrets → Actions) and reference them as ${{ secrets.SECRET_NAME }}. They are masked in logs and unavailable to forked pull requests. For ML pipelines, store MLflow credentials, cloud storage keys, and database connection strings as secrets.
# .github/workflows/retrain.yml -- env section with secrets
# jobs:
# retrain:
# runs-on: ubuntu-latest
# env:
# MLFLOW_TRACKING_URI: ${{ secrets.MLFLOW_TRACKING_URI }}
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}
#
# In train.py, read normally:
import os
DB_URL = os.getenv('DATABASE_URL')
MLFLOW_URI = os.getenv('MLFLOW_TRACKING_URI')
print('Config loaded from environment.')The Training Step
The core step runs your Python training script. The script should accept configuration via environment variables (set in the workflow), log all parameters and metrics to MLflow, and write the new model to the registry as a new version in None stage. Keep the script idempotent: running it twice with the same data should produce a new registry version each time without corrupting existing versions.
# train.py -- called by GitHub Actions
import os
import mlflow
import mlflow.sklearn
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_score
mlflow.set_tracking_uri(os.getenv('MLFLOW_TRACKING_URI', 'http://localhost:5000'))
mlflow.set_experiment('automated_retraining')
with mlflow.start_run(run_name='scheduled_retrain'):
n_est = int(os.getenv('N_ESTIMATORS', '200'))
lr = float(os.getenv('LEARNING_RATE', '0.05'))
mlflow.log_param('n_estimators', n_est)
mlflow.log_param('learning_rate', lr)
clf = GradientBoostingClassifier(n_estimators=n_est, learning_rate=lr, random_state=42)
# clf.fit(X_train, y_train)
# f1 = f1_score(y_test, clf.predict(X_test))
# mlflow.log_metric('test_f1', f1)
# mlflow.sklearn.log_model(clf, 'model', registered_model_name='SentimentClassifier')
print('Training step complete.')The Evaluation Step: Beat the Champion
After training, an evaluation step compares the new model against the current Production champion. If the new model wins, it proceeds to promotion; otherwise the pipeline marks the new version as rejected (tag it with status=rejected) and keeps the champion in Production. Use a GitHub Actions output variable to pass the promotion decision between steps.
# evaluate.py -- compare new version against production
from mlflow.tracking import MlflowClient
import sys
client = MlflowClient()
MODEL_NAME = 'SentimentClassifier'
def get_f1(run_id):
return client.get_run(run_id).data.metrics.get('test_f1', 0)
# Get latest registered version (just added by train.py)
new_versions = client.get_latest_versions(MODEL_NAME, stages=['None'])
new_v = sorted(new_versions, key=lambda v: int(v.version))[-1]
new_f1 = get_f1(new_v.run_id)
prod_versions = client.get_latest_versions(MODEL_NAME, stages=['Production'])
if prod_versions:
prod_f1 = get_f1(prod_versions[0].run_id)
print(f'Champion F1: {prod_f1:.4f} Challenger F1: {new_f1:.4f}')
should_promote = new_f1 > prod_f1
else:
print('No champion found.')
should_promote = True
print('should_promote=' + str(should_promote).lower())
sys.exit(0 if should_promote else 1) # exit code drives next stepThe Promotion Step: Conditional Execution
Use GitHub Actions' if: success() and if: failure() conditions on steps or jobs to implement conditional promotion. After the evaluation step, a promotion step runs only if evaluation succeeded (the challenger won). Use a separate notification step that always runs to send a Slack or email alert with the pipeline outcome, regardless of whether promotion happened.
# promote.py -- transition new version to Production
from mlflow.tracking import MlflowClient
client = MlflowClient()
MODEL_NAME = 'SentimentClassifier'
new_versions = client.get_latest_versions(MODEL_NAME, stages=['None'])
new_v = sorted(new_versions, key=lambda v: int(v.version))[-1]
client.transition_model_version_stage(
name=MODEL_NAME,
version=new_v.version,
stage='Production',
archive_existing_versions=True
)
print(f'Version {new_v.version} promoted to Production.')
# In the workflow YAML:
# - name: Promote model
# if: success() # only runs if evaluate.py exits with 0
# run: python promote.pyData Download in the Pipeline
Retraining pipelines need fresh data. Add a data download step before training that fetches the latest data from S3, a database, or a data lake. Use versioned data snapshots (tagged with a date) rather than always pulling the latest to maintain reproducibility: if a model performs poorly, you need to reproduce the exact training data it saw. Log the data version as an MLflow parameter alongside model hyperparameters.
# download_data.py -- fetch latest training data from S3
import boto3
import os
from datetime import date
DATA_DATE = os.getenv('DATA_DATE', str(date.today())) # e.g., 2024-06-15
BUCKET = os.getenv('S3_BUCKET', 'my-ml-data')
KEY = f'datasets/sentiment/{DATA_DATE}/train.parquet'
s3 = boto3.client('s3')
os.makedirs('/tmp/data', exist_ok=True)
s3.download_file(BUCKET, KEY, '/tmp/data/train.parquet')
print(f'Downloaded data for {DATA_DATE}')
# In train.py, log data_date as a parameter:
# mlflow.log_param('data_date', DATA_DATE)Notifications with Slack Webhooks
An automated pipeline without notifications is a black box. Add a final notification step that always runs (using if: always()) and posts a summary to Slack or sends an email. Include the run outcome (promoted or rejected), the new version number, the F1 scores of both champion and challenger, and a direct link to the MLflow run for drill-down. Good notifications turn silent automation into a collaborative data science workflow.
import os
import requests
import json
SLACK_WEBHOOK = os.getenv('SLACK_WEBHOOK_URL')
PROMOTED = os.getenv('PROMOTED', 'false') == 'true'
NEW_VERSION = os.getenv('NEW_VERSION', 'N/A')
NEW_F1 = os.getenv('NEW_F1', 'N/A')
status_icon = ':white_check_mark:' if PROMOTED else ':x:'
status_text = 'Promoted to Production' if PROMOTED else 'Champion retained'
message = {
'text': (f'{status_icon} *ML Retraining Pipeline*\n'
f'Model: SentimentClassifier v{NEW_VERSION}\n'
f'Status: {status_text}\n'
f'New F1: {NEW_F1}')
}
if SLACK_WEBHOOK:
requests.post(SLACK_WEBHOOK, data=json.dumps(message))
print('Slack notification sent.')Complete Workflow YAML Overview
A complete automated retraining workflow has five sequential jobs: download_data (fetch latest dataset), train (run training script, log to MLflow), evaluate (compare vs production champion), promote (transition stage if challenger wins), and notify (always-run Slack alert). Each job passes outputs to the next using GitHub Actions job outputs or shared artifact files. The entire pipeline runs unattended on a weekly schedule.
# Full workflow summary (pseudo-YAML)
# name: Weekly Model Retraining
# on:
# schedule: [{cron: '0 3 * * 1'}]
# workflow_dispatch: {}
# jobs:
# download_data:
# runs-on: ubuntu-latest
# steps: [checkout, setup-python, install, run download_data.py]
# train:
# needs: download_data
# steps: [checkout, setup-python, install, run train.py]
# evaluate:
# needs: train
# steps: [checkout, setup-python, install, run evaluate.py]
# promote:
# needs: evaluate
# if: success()
# steps: [checkout, setup-python, install, run promote.py]
# notify:
# needs: [evaluate, promote]
# if: always()
# steps: [run notify.py]
print('Full workflow YAML structure shown above.')Quick Check
Test your understanding of Machine Learning with Python concepts from this lesson.
Lesson Recap
In this lesson you learned: GitHub Actions cron triggers run retraining on a schedule without manual intervention, evaluation scripts implement the champion/challenger pattern so only better models are promoted, and secrets management keeps credentials out of workflow YAML files using encrypted GitHub Secrets. Next up we explore data drift detection — identifying when the input distribution shifts away from training conditions, which is the most common cause of production model degradation.
常见问题解答
「使用 GitHub Actions 实现自动重新训练管道」课时是免费的吗?
是的 — 「使用 GitHub Actions 实现自动重新训练管道」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Machine Learning Academy 课程的其余内容,请升级到 CoddyKit PRO。 Machine Learning Academy 课程共包含 4 节课。
「使用 GitHub Actions 实现自动重新训练管道」这节课中我会学到什么?
您将编写 GitHub Actions 工作流,使其按数据计划触发训练、评估新模型,并仅在新模型优于生产基线时将其晋级。 你通过在浏览器中直接运行的动手代码来练习 Machine Learning Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Machine Learning Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Machine Learning Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 GitHub Actions 实现自动重新训练管道」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Machine Learning Academy 课中编写并运行代码吗?
能。每节 Machine Learning Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 MLflow 跟踪实验:记录参数、指标与制品
- 使用 Docker 为机器学习构建可复现环境
- 模型注册表:暂存、生产与归档
- 使用 GitHub Actions 实现自动重新训练管道