Intermediate

CI/CD Pipeline

Automate training, validation, and deployment with GitHub Actions.

GitHub Actions Workflow

# .github/workflows/train.yml
name: MLOps Pipeline

on:
  push:
    branches: [main]
    paths: [src/**, data/**, params.yaml]
  workflow_dispatch:

jobs:
  train:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: pip install -r requirements.txt

      - name: Pull data with DVC
        run: dvc pull
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

      - name: Run DVC pipeline
        run: dvc repro

      - name: Log to MLflow
        run: python src/train.py
        env:
          MLFLOW_TRACKING_URI: ${{ secrets.MLFLOW_URI }}

      - name: Validate model
        run: |
          python -c "
          import json
          with open('metrics.json') as f:
              m = json.load(f)
          assert m['accuracy'] >= 0.85, f'Accuracy {m["accuracy"]} below 0.85'
          assert m['f1'] >= 0.80, f'F1 {m["f1"]} below 0.80'
          print('Validation passed!')
          "

      - name: Register model
        if: success()
        run: python src/registry.py
        env:
          MLFLOW_TRACKING_URI: ${{ secrets.MLFLOW_URI }}

  deploy:
    needs: train
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t mlops-model:latest .

      - name: Push to registry
        run: |
          docker tag mlops-model:latest ${{ secrets.REGISTRY }}/mlops-model:latest
          docker push ${{ secrets.REGISTRY }}/mlops-model:latest

      - name: Deploy
        run: |
          echo "Deploying to production..."
          # kubectl apply -f k8s/deployment.yaml
          # Or: docker-compose up -d
💡
Validation gates: The pipeline fails if accuracy drops below 0.85 or F1 below 0.80, preventing bad models from reaching production.