Library

Volume 1 · CI/CD, DevOps & Observability

Notebook
Sign in

Section 31

CI/CD Pipeline Architecture (YAML pipeline examples for AI/ML workloads)

1,022 words · about 5 min

31.1 Modern CI/CD Pipeline — End to End

At the architect level, CI/CD is not “does the build pass” — it is a governed, staged promotion system with security gates, automated rollback, and progressive exposure. The general shape:

Developer Push → Source Control (Git)
    │
    ▼ CI Pipeline (Automated, triggered on PR/merge)
┌──────────────────────────────────────────────────────┐
│ 1. Restore dependencies                              │
│ 2. Build                                              │
│ 3. Static analysis / lint (SonarQube, Roslyn, ESLint)│
│ 4. Unit tests (fast, isolated)                       │
│ 5. Security scan (SAST — Snyk, Checkmarx, CodeQL)    │
│ 6. Dependency/SCA scan (OWASP Dependency-Check)       │
│ 7. Container build + image scan (Trivy, Grype)       │
│ 8. Push image to registry (ECR/ACR/GCR)               │
│ 9. Sign artifact (cosign / Notary) + SBOM generation  │
└──────────────────────┬───────────────────────────────┘
                       │
    ▼ CD Pipeline (Staged promotion)
┌──────────────────────▼───────────────────────────────┐
│ Dev      → Auto-deploy on merge to develop            │
│ Staging  → Auto-deploy on merge to main, run          │
│            integration + E2E + load tests             │
│ Prod     → Manual approval gate → Canary deploy       │
│          → Smoke tests → Progressive traffic shift    │
│          → Automated rollback on SLO breach            │
└────────────────────────────────────────────────────────┘

31.2 GitHub Actions — General Application Pipeline

name: CI/CD Pipeline
on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-dotnet@v4
      with:
        dotnet-version: '8.0.x'

    - name: Restore
      run: dotnet restore

    - name: Build
      run: dotnet build --no-restore -c Release

    - name: Unit Tests
      run: dotnet test --no-build -c Release --filter "Category=Unit"
           --logger "trx" --collect:"XPlat Code Coverage"

    - name: Integration Tests
      run: dotnet test --no-build -c Release --filter "Category=Integration"
      env:
        ConnectionStrings__Test: ${{ secrets.TEST_DB_CONNECTION }}

    - name: Publish Code Coverage
      uses: codecov/codecov-action@v4

  docker-build:
    needs: build-and-test
    runs-on: ubuntu-latest
    steps:
    - name: Build and Push Docker Image
      uses: docker/build-push-action@v5
      with:
        push: true
        tags: myregistry.azurecr.io/api:${{ github.sha }}

  deploy-staging:
    needs: docker-build
    if: github.ref == 'refs/heads/main'
    environment: staging
    runs-on: ubuntu-latest
    steps:
    - name: Deploy to Container Apps
      uses: azure/container-apps-deploy-action@v1
      with:
        imageToDeploy: myregistry.azurecr.io/api:${{ github.sha }}

  deploy-production:
    needs: deploy-staging
    environment:
      name: production
      url: https://api.myapp.com
    runs-on: ubuntu-latest
    steps:
    - name: Deploy to Production
      uses: azure/container-apps-deploy-action@v1
    - name: Smoke Test
      run: curl -f https://api.myapp.com/health || exit 1

31.3 CI/CD Pipeline for AI/ML & LLM Workloads

AI/ML pipelines need everything above PLUS model-specific gates: data validation, offline evaluation, bias/fairness checks, and shadow/canary rollout tied to quality metrics, not just uptime.

name: ML Model CI/CD
on:
  push:
    paths:
      - 'models/**'
      - 'prompts/**'
      - 'src/**'

jobs:
  data-validation:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Validate training/eval data schema
        run: great_expectations checkpoint run claims_data_checkpoint
      - name: Check for data drift vs. baseline
        run: python scripts/drift_check.py --baseline s3://model-baselines/v1

  train-and-evaluate:
    needs: data-validation
    runs-on: [self-hosted, gpu]
    steps:
      - name: Train / fine-tune
        run: python train.py --config configs/prod.yaml
      - name: Log to MLflow
        run: python -c "import mlflow; mlflow.log_artifacts('outputs/')"
      - name: Offline evaluation (accuracy, RAGAS, toxicity)
        run: python evaluate.py --golden-set data/golden_v3.jsonl
      - name: Fairness / bias gate
        run: python fairness_check.py --threshold 0.05
      - name: Fail pipeline if regression vs. production model
        run: python compare_models.py --candidate outputs/model --baseline prod

  package-and-scan:
    needs: train-and-evaluate
    runs-on: ubuntu-latest
    steps:
      - name: Build inference container
        run: docker build -t registry/model-server:${{ github.sha }} .
      - name: Scan image for vulnerabilities
        run: trivy image registry/model-server:${{ github.sha }}
      - name: Generate model card + SBOM
        run: python generate_model_card.py

  shadow-deploy:
    needs: package-and-scan
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to shadow endpoint (mirrors prod traffic, no user impact)
        run: kubectl apply -f k8s/shadow-deployment.yaml
      - name: Monitor shadow metrics for 24h
        run: python monitor_shadow.py --duration 24h

  canary-promote:
    needs: shadow-deploy
    environment:
      name: production
    runs-on: ubuntu-latest
    steps:
      - name: Canary rollout via Argo Rollouts (5% → 25% → 100%)
        run: kubectl argo rollouts promote model-server
      - name: Auto-rollback if latency/error-rate/quality SLO breached
        run: python watch_canary_slo.py --rollout model-server

31.4 Infrastructure as Code (Terraform, CloudFormation, Bicep)

Principles: version control everything; immutable infrastructure (replace, don’t patch); idempotency; modular reusable modules; environment parity via parameter files; remote locked state; scheduled drift detection (terraform plan).

Terraform module — reusable container app:

# modules/containerapp/main.tf
variable "name" {}
variable "image" {}
variable "environment_id" {}
variable "cpu" { default = 0.5 }
variable "memory" { default = "1Gi" }
variable "min_replicas" { default = 1 }
variable "max_replicas" { default = 10 }

resource "azurerm_container_app" "app" {
  name                         = var.name
  container_app_environment_id = var.environment_id
  resource_group_name          = var.resource_group_name

  template {
    container {
      name   = var.name
      image  = var.image
      cpu    = var.cpu
      memory = var.memory
    }
    min_replicas = var.min_replicas
    max_replicas = var.max_replicas
  }
}

# Usage
module "order_api" {
  source         = "./modules/containerapp"
  name           = "order-api"
  image          = "myregistry.azurecr.io/order-api:v1.2.3"
  environment_id = module.environment.id
  min_replicas   = 2
  max_replicas   = 20
}

AWS CloudFormation — inference endpoint stack (illustrative):

Resources:
  ModelEndpointConfig:
    Type: AWS::SageMaker::EndpointConfig
    Properties:
      ProductionVariants:
        - VariantName: primary
          ModelName: !Ref ModelResource
          InstanceType: ml.g5.2xlarge
          InitialInstanceCount: 2
  ModelEndpoint:
    Type: AWS::SageMaker::Endpoint
    Properties:
      EndpointConfigName: !Ref ModelEndpointConfig

Azure Bicep — Container App environment:

resource containerAppEnv 'Microsoft.App/managedEnvironments@2023-05-01' = {
  name: 'ai-inference-env'
  location: location
  properties: {
    appLogsConfiguration: {
      destination: 'log-analytics'
      logAnalyticsConfiguration: {
        customerId: logAnalyticsWorkspace.properties.customerId
      }
    }
  }
}

Terraform vs. CloudFormation vs. Bicep:

Dimension Terraform CloudFormation Bicep
Cloud scope Multi-cloud AWS-only Azure-only
Language HCL JSON/YAML DSL (compiles to ARM)
State Explicit remote state file Managed by AWS Managed by Azure
Modularity Strong (modules, registry) Nested stacks Modules
Drift detection terraform plan Drift detection API What-if deployment
Best for Multi-cloud/hybrid orgs AWS-native shops Azure-native shops