CI/CD Pipeline Design
This design outlines a robust, scalable CI/CD pipeline for a containerized application (e.g., a Node.js or Go microservice). It uses GitHub Actions for orchestration, Docker for packaging, Kubernetes (K8s) for deployment, and Helm for managing K8s manifests. The pipeline supports automated testing, artifact promotion, progressive deployments (canary/blue-green), rollbacks, and manual approvals.
Key principles:
- Security: Secrets in GitHub Secrets/OIDC for cloud providers (e.g., EKS).
- Observability: Integrate Prometheus/Grafana, Datadog for metrics; logs via ELK.
- Idempotency: All steps are repeatable.
- Cost Efficiency: Use ephemeral runners; cache layers.
1. Branching Strategy (Trunk-Based with Release Candidates)
Use Trunk-Based Development for speed, with short-lived feature branches:
main : Production-ready (protected; tags for releases, e.g., v1.2.3)
develop : Integration branch (default target for PRs)
feature/* : Short-lived (<2 days); branch from develop, PR to develop
hotfix/* : Urgent fixes from main, PR to main + develop
release/* : From develop; stabilize, tag, merge to main/develop
This minimizes merge conflicts, enables fast feedback.
2. Build/Test Stages
Pipeline runs on:
- Push/PR to
develop: Build/test only.
- Push/PR to
main/release/*: Full pipeline + deploy to staging/prod.
Stages (parallel where possible):
- Lint (fast-fail): ESLint/Go vet.
- Unit Tests: Jest/Go test; coverage >80%.
- Build: Docker build; multi-arch if needed.
- Integration Tests: Spin up Testcontainers; against artifact.
- Security Scan: Trivy/Snyk for vulnerabilities.
- E2E Tests: Playwright/Cypress on staging-like env.
- Publish Artifact: Only on success (Docker push + Helm chart).
Caching: Docker layers, npm/yarn deps.
3. Artifacts
- Docker Image:
ghcr.io/org/app:${{ github.sha }} (immutable tag) + latest/tag for branches.
- Helm Chart: Packaged as OCI artifact in GHCR/Artifact Hub.
- Provenance: Use
cosign for signing (SLSA compliance).
- Retention: 90 days for non-prod; indefinite for tagged releases.
Promotion: Images auto-promote via tags (e.g., develop-latest → staging-latest on merge).
4. Environment Promotion
Environments: Dev (auto) → Staging (auto on release/*) → Prod (manual).
| Env | Trigger | Deploy Type | Gate |
|---|
| Dev | Push to develop | Full (fast) | Tests only |
| Staging | Merge to release/* | Canary | Manual approval + smoke tests |
| Prod | Tag on main | Blue-Green | 2 approvals + business gate |
Use Helm for deployments:
# values-prod.yaml
image:
tag: "{{ .Values.imageTag }}" # e.g., v1.2.3
replicaCount: 10
5. Deployment Strategies
Canary (Staging)
- Roll out 10% traffic → Monitor 5min → 50% → 100%.
- K8s: VirtualService + Istio/Flagger for traffic splitting.
- Metrics: Error rate <1%, latency <P95 500ms.
Blue-Green (Prod)
- Blue: Live env.
- Green: New version deployed in parallel.
- Switch traffic (Ingress/ALB), monitor 10min, delete blue.
- K8s: Two namespaces (
prod-blue, prod-green); Helm release toggle.
Rollback integrated: Switch back to blue or previous tag.
Tools: Flagger/Argo Rollouts for automation.
6. Rollback & Approvals
Rollback
- Immediate:
helm rollback RELEASE_NAME PREV_REVISION.
- Automated: On failed health checks (K8s HPA + readiness probes).
- Git-Based: Revert merge/PR; re-trigger pipeline.
- Blue-Green: Traffic switch (zero-downtime).
Approvals
- GitHub Environments: Require reviewers (e.g., 2 for prod).
- Business Gates: Jira ticket link in PR description.
Example GitHub Actions Config
Single workflow: .github/workflows/ci-cd.yml
name: CI/CD Pipeline
on:
push:
branches: [develop, main, release/**]
pull_request:
branches: [develop, main]
env:
REGISTRY: ghcr.io/${{ github.repository_owner }}/app
IMAGE_TAG: ${{ github.sha }}
HELM_CHART: ./charts/app
jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4 # or golang
with: { node-version: 20 }
- run: npm ci # yarn install
- run: npm run lint
- run: npm test -- --coverage # Unit tests
build:
needs: lint-test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # GHCR push
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ env.REGISTRY }}:${{ env.IMAGE_TAG }}
${{ env.REGISTRY }}:${{ github.ref_name }}-latest # e.g., develop-latest
cache-from: type=gha
cache-to: type=gha,mode=max
- uses: sigstore/cosign-installer@v3 # Sign image
- run: cosign sign ${{ env.REGISTRY }}@${{ env.IMAGE_TAG }} # w/ OIDC
security-scan:
needs: build
runs-on: ubuntu-latest
steps:
- uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}:${{ env.IMAGE_TAG }}
format: 'sarif'
output: 'trivy-results.sarif'
- uses: github/codeql-action/upload-sarif@v3
if: always()
with: { sarif_file: 'trivy-results.sarif' }
# Parallel: integration + e2e (omitted for brevity; use Testcontainers)
deploy-dev:
needs: [build, security-scan]
if: github.ref == 'refs/heads/develop'
uses: ./.github/workflows/reusable-deploy.yml
with:
env: dev
image-tag: ${{ env.IMAGE_TAG }}
secrets: inherit
deploy-staging:
needs: [build, security-scan]
if: startsWith(github.ref, 'refs/heads/release/')
environment: staging # Triggers approval
uses: ./.github/workflows/reusable-deploy.yml
with:
env: staging
image-tag: ${{ env.IMAGE_TAG }}
deploy-type: canary
secrets: inherit
deploy-prod:
needs: deploy-staging # Sequential
if: startsWith(github.ref, 'refs/tags/v')
environment: production # 2 approvals required
uses: ./.github/workflows/reusable-deploy.yml
with:
env: prod
image-tag: ${{ github.ref_name }} # v1.2.3
deploy-type: blue-green
secrets: inherit
Reusable Deploy Workflow (.github/workflows/reusable-deploy.yml)
name: Deploy to K8s
on:
workflow_call:
inputs:
env:
required: true
image-tag:
required: true
deploy-type:
default: full
secrets:
KUBE_CONFIG:
required: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Helm
uses: azure/setup-helm@v4
- name: Login K8s
uses: azure/k8s-set-context@v4
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBE_CONFIG }}
- name: Package & Deploy Helm
run: |
helm package ${{ env.HELM_CHART }}
helm upgrade --install app-${{ inputs.env }} app-*.tgz \
--namespace ${{ inputs.env }} --create-namespace \
--set image.tag=${{ inputs.image-tag }} \
--set deployType=${{ inputs.deploy-type }} # Custom values for canary/blue-green
- name: Smoke Tests
run: kubectl wait --for=condition=Available deployment/app-${{ inputs.env }} --timeout=300s
- name: Rollout Analysis (Canary/Blue-Green)
if: inputs.deploy-type != 'full'
uses: fluxcd/flux2/action@v2 # Or Flagger promote
with:
install-latest: true
Monitoring & Post-Deployment
- Health Checks: K8s probes + custom
/healthz.
- Alerts: Prometheus Alertmanager on deploy.
- Cleanup: Expired images via GitHub Packages retention.
This pipeline achieves zero-downtime deploys, <10min feedback loops, and audit trails. Scale by adding jobs (e.g., for monorepo). Customize for your stack (e.g., Terraform for infra). Questions?