CI/CD Pipelines for Airflow
Architecture Diagram
Formal Definitions
Detailed Explanation
Why CI/CD for Airflow?
CI/CD pipelines ensure that DAG changes are tested, validated, and deployed safely before reaching production. This prevents broken DAGs from disrupting production workflows.
Critical Point: Without CI/CD, a simple syntax error in a DAG file can crash the scheduler and affect all running workflows.
CI/CD Pipeline Stages
| Stage | Purpose | Tools | Duration |
|---|---|---|---|
| Lint | Code quality checks | flake8, black, isort | 1-2 min |
| Test | Validate DAG logic | pytest, Docker | 5-15 min |
| Build | Create deployable artifacts | Docker, Helm | 5-10 min |
| Deploy | Push to environments | ArgoCD, kubectl | 2-5 min |
| Verify | Post-deploy health checks | Custom scripts | 1-3 min |
CI Pipeline Configuration
# .github/workflows/airflow-ci.yml
name: Airflow CI
on:
push:
branches: [main, develop]
paths:
- 'dags/**'
- 'plugins/**'
- 'tests/**'
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
pip install flake8 black isort mypy pylint
- name: Lint with flake8
run: flake8 dags/ plugins/ tests/ --max-line-length=120
- name: Check formatting with black
run: black --check dags/ plugins/ tests/
- name: Sort imports
run: isort --check-only dags/ plugins/ tests/
- name: Type check with mypy
run: mypy dags/ --ignore-missing-imports
test:
runs-on: ubuntu-latest
needs: lint
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: airflow
POSTGRES_PASSWORD: airflow
POSTGRES_DB: airflow
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install Airflow
run: |
pip install apache-airflow==2.8.0
pip install pytest pytest-cov pytest-xdist
pip install -r requirements.txt
- name: Run DAG validation
run: |
pytest tests/test_dag_validation.py -v --tb=short
- name: Run unit tests
run: |
pytest tests/unit/ -v --cov=dags --cov-report=xml -n auto
- name: Run integration tests
run: |
pytest tests/integration/ -v --tb=short
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
build-image:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: |
docker build -t airflow-dags:${{ github.sha }} .
docker tag airflow-dags:${{ github.sha }} airflow-dags:latest
- name: Push to registry
run: |
echo ${{ secrets.REGISTRY_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin
docker push airflow-dags:${{ github.sha }}
docker push airflow-dags:latest
DAG Testing Framework
Deployment Automation
Deployment Strategies
| Strategy | Description | Rollback | Complexity |
|---|---|---|---|
| Direct Copy | Copy files to DAG folder | Manual | Low |
| Git-Sync | GitOps with automated sync | Git revert | Medium |
| Docker Image | Containerized DAGs | Image rollback | Medium |
| Helm Chart | Kubernetes-native deployment | Helm rollback | High |
| ArgoCD | GitOps with visual UI | Automatic | High |
Key Benefits of CI/CD
- Automated Testing: Catch bugs before they reach production
- Version Control: Track all DAG changes with Git history
- Rollback Capability: Quickly revert to previous working state
- Consistent Deployments: Eliminate manual deployment errors
- Audit Trail: Know who deployed what and when
Key Concepts Table
| Stage | Tool | Purpose | Duration |
|---|---|---|---|
| Lint | flake8, black | Code quality | 1-2min |
| Unit Test | pytest | Component testing | 2-5min |
| Integration Test | pytest + Docker | System testing | 5-15min |
| Build | Docker | Image creation | 5-10min |
| Deploy | Helm/Kubectl | Production deploy | 2-5min |
| Verify | Health checks | Post-deploy validation | 1-3min |
Code Examples
GitOps with ArgoCD
# argocd-application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: airflow-dags
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/airflow-dags.git
targetRevision: HEAD
path: dags
helm:
valueFiles:
- values.yaml
destination:
server: https://kubernetes.default.svc
namespace: airflow
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
Automated Testing Pipeline
Rollback Automation
Performance Metrics
CI/CD Pipeline Performance
| Metric | Target | Warning | Critical |
|---|---|---|---|
| CI Duration | < 10min | 10-20min | > 20min |
| CD Duration | < 5min | 5-10min | > 10min |
| Deployment Success | > 99% | 95-99% | < 95% |
| Rollback Time | < 2min | 2-5min | > 5min |
| Test Coverage | > 80% | 70-80% | < 70% |
Deployment Frequency
| Environment | Frequency | Approval | Automation |
|---|---|---|---|
| Development | On push | Auto | 100% |
| Staging | On PR merge | Auto | 90% |
| Production | On release | Manual | 70% |
See Also
- Testing DAGs â Comprehensive testing strategies
- Performance Tuning â Optimizing CI/CD pipeline speed
- Kubernetes Executor â Containerized deployment
- Monitoring and Alerting â Post-deployment monitoring