Tutorial Lengkap GitHub Actions untuk ML CI/CD
GitHub Actions memungkinkan Anda mengotomasi workflow Machine Learning dari testing hingga deployment. Dalam tutorial ini, kita akan membangun CI/CD pipeline yang komprehensif untuk ML projects, termasuk data validation, model training, testing, dan deployment.
Mengapa CI/CD untuk ML?
Tantangan dalam ML projects:
- Reproducibility: Memastikan hasil konsisten di berbagai environment
- Testing: Validasi data, model, dan code
- Automation: Mengurangi manual work dan human error
- Collaboration: Standardisasi workflow antar tim
- Monitoring: Track performance dan detect regressions
- Automated testing pada setiap push/PR
- Scheduled retraining
- Model validation gates
- Automated deployment
- Integration dengan cloud services
GitHub Actions Basics
1. Struktur Workflow File
# .github/workflows/ml-pipeline.yml
name: ML Pipeline
Triggers
on:
push:
branches: [main, develop]
pullrequest:
branches: [main]
schedule:
- cron: '0 0 0' # Weekly on Sunday
workflowdispatch: # Manual trigger
Environment variables
env:
PYTHONVERSION: '3.10'
MODELNAME: 'my-model'
Jobs
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHONVERSION }}
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest tests/
2. Workflow Triggers
on:
# Push to specific branches
push:
branches: [main]
paths:
- 'src/'
- 'tests/'
- 'requirements.txt'
# Pull requests
pullrequest:
branches: [main]
# Scheduled runs
schedule:
- cron: '0 2 ' # Daily at 2 AM UTC
# Manual trigger dengan inputs
workflowdispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'staging'
type: choice
options:
- staging
- production
retrain:
description: 'Force retrain model'
required: false
type: boolean
default: false
ML Testing Pipeline
1. Code Quality dan Unit Tests
name: Code Quality & Tests
on:
push:
branches: [main, develop]
pullrequest:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install linters
run: |
pip install flake8 black isort mypy
- name: Run flake8
run: flake8 src/ tests/
- name: Check black formatting
run: black --check src/ tests/
- name: Check import sorting
run: isort --check-only src/ tests/
- name: Run mypy
run: mypy src/
test:
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run unit tests
run: pytest tests/unit/ -v --cov=src --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml