$npx -y skills add softspark/ai-toolkit --skill ci-cd-patternsCI/CD: GitHub Actions, GitLab CI, Jenkins, caching, blue-green, canary. Triggers: CI, CD, pipeline, GitHub Actions, workflow YAML, release, canary, rollout.
| 1 | # CI/CD Patterns |
| 2 | |
| 3 | ## GitHub Actions |
| 4 | |
| 5 | ### Standard Pipeline |
| 6 | ```yaml |
| 7 | name: CI |
| 8 | on: |
| 9 | push: |
| 10 | branches: [main] |
| 11 | pull_request: |
| 12 | branches: [main] |
| 13 | |
| 14 | jobs: |
| 15 | lint: |
| 16 | runs-on: ubuntu-latest |
| 17 | steps: |
| 18 | - uses: actions/checkout@v6 |
| 19 | - uses: actions/setup-node@v6 |
| 20 | with: |
| 21 | node-version: 20 |
| 22 | cache: "npm" |
| 23 | - run: npm ci |
| 24 | - run: npm run lint |
| 25 | - run: npm run typecheck |
| 26 | |
| 27 | test: |
| 28 | runs-on: ubuntu-latest |
| 29 | needs: lint |
| 30 | strategy: |
| 31 | matrix: |
| 32 | node-version: [18, 20, 22] |
| 33 | steps: |
| 34 | - uses: actions/checkout@v6 |
| 35 | - uses: actions/setup-node@v6 |
| 36 | with: |
| 37 | node-version: ${{ matrix.node-version }} |
| 38 | cache: "npm" |
| 39 | - run: npm ci |
| 40 | - run: npm test -- --coverage |
| 41 | - uses: actions/upload-artifact@v4 |
| 42 | with: |
| 43 | name: coverage-${{ matrix.node-version }} |
| 44 | path: coverage/ |
| 45 | |
| 46 | build: |
| 47 | runs-on: ubuntu-latest |
| 48 | needs: test |
| 49 | steps: |
| 50 | - uses: actions/checkout@v6 |
| 51 | - uses: actions/setup-node@v6 |
| 52 | with: |
| 53 | node-version: 20 |
| 54 | cache: "npm" |
| 55 | - run: npm ci |
| 56 | - run: npm run build |
| 57 | ``` |
| 58 | |
| 59 | ### Python CI |
| 60 | ```yaml |
| 61 | name: Python CI |
| 62 | on: [push, pull_request] |
| 63 | |
| 64 | jobs: |
| 65 | test: |
| 66 | runs-on: ubuntu-latest |
| 67 | steps: |
| 68 | - uses: actions/checkout@v6 |
| 69 | - uses: actions/setup-python@v5 |
| 70 | with: |
| 71 | python-version: "3.12" |
| 72 | cache: "pip" |
| 73 | - run: pip install -e ".[dev]" |
| 74 | - run: ruff check . |
| 75 | - run: mypy --strict src/ |
| 76 | - run: pytest --cov=src --cov-report=xml |
| 77 | ``` |
| 78 | |
| 79 | ### Docker Build & Push |
| 80 | ```yaml |
| 81 | build-docker: |
| 82 | runs-on: ubuntu-latest |
| 83 | needs: test |
| 84 | steps: |
| 85 | - uses: actions/checkout@v6 |
| 86 | - uses: docker/setup-buildx-action@v3 |
| 87 | - uses: docker/login-action@v3 |
| 88 | with: |
| 89 | registry: ghcr.io |
| 90 | username: ${{ github.actor }} |
| 91 | password: ${{ secrets.GITHUB_TOKEN }} |
| 92 | - uses: docker/build-push-action@v5 |
| 93 | with: |
| 94 | context: . |
| 95 | push: ${{ github.ref == 'refs/heads/main' }} |
| 96 | tags: ghcr.io/${{ github.repository }}:${{ github.sha }} |
| 97 | cache-from: type=gha |
| 98 | cache-to: type=gha,mode=max |
| 99 | ``` |
| 100 | |
| 101 | ## GitLab CI |
| 102 | |
| 103 | ```yaml |
| 104 | stages: |
| 105 | - lint |
| 106 | - test |
| 107 | - build |
| 108 | - deploy |
| 109 | |
| 110 | lint: |
| 111 | stage: lint |
| 112 | image: node:20 |
| 113 | cache: |
| 114 | key: $CI_COMMIT_REF_SLUG |
| 115 | paths: [node_modules/] |
| 116 | script: |
| 117 | - npm ci |
| 118 | - npm run lint |
| 119 | |
| 120 | test: |
| 121 | stage: test |
| 122 | image: node:20 |
| 123 | services: |
| 124 | - postgres:16 |
| 125 | variables: |
| 126 | DATABASE_URL: "postgresql://postgres:postgres@postgres/test" |
| 127 | script: |
| 128 | - npm ci |
| 129 | - npm test |
| 130 | |
| 131 | build: |
| 132 | stage: build |
| 133 | image: docker:24 |
| 134 | services: |
| 135 | - docker:24-dind |
| 136 | script: |
| 137 | - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . |
| 138 | - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 139 | only: |
| 140 | - main |
| 141 | ``` |
| 142 | |
| 143 | ## Docker Multi-stage Builds |
| 144 | |
| 145 | ### Node.js |
| 146 | ```dockerfile |
| 147 | # Stage 1: Dependencies |
| 148 | FROM node:20-alpine AS deps |
| 149 | WORKDIR /app |
| 150 | COPY package.json package-lock.json ./ |
| 151 | RUN npm ci --production |
| 152 | |
| 153 | # Stage 2: Build |
| 154 | FROM node:20-alpine AS build |
| 155 | WORKDIR /app |
| 156 | COPY package.json package-lock.json ./ |
| 157 | RUN npm ci |
| 158 | COPY . . |
| 159 | RUN npm run build |
| 160 | |
| 161 | # Stage 3: Production |
| 162 | FROM node:20-alpine AS production |
| 163 | WORKDIR /app |
| 164 | ENV NODE_ENV=production |
| 165 | COPY --from=deps /app/node_modules ./node_modules |
| 166 | COPY --from=build /app/dist ./dist |
| 167 | USER node |
| 168 | EXPOSE 3000 |
| 169 | CMD ["node", "dist/index.js"] |
| 170 | ``` |
| 171 | |
| 172 | ### Python |
| 173 | ```dockerfile |
| 174 | FROM python:3.12-slim AS builder |
| 175 | WORKDIR /app |
| 176 | COPY pyproject.toml . |
| 177 | RUN pip install --no-cache-dir --target=/deps . |
| 178 | |
| 179 | FROM python:3.12-slim |
| 180 | WORKDIR /app |
| 181 | COPY --from=builder /deps /usr/local/lib/python3.12/site-packages |
| 182 | COPY src/ ./src/ |
| 183 | USER nobody |
| 184 | CMD ["python", "-m", "src.main"] |
| 185 | ``` |
| 186 | |
| 187 | ## Caching Strategies |
| 188 | |
| 189 | ### npm/pnpm |
| 190 | ```yaml |
| 191 | - uses: actions/cache@v4 |
| 192 | with: |
| 193 | path: ~/.npm |
| 194 | key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} |
| 195 | restore-keys: ${{ runner.os }}-npm- |
| 196 | ``` |
| 197 | |
| 198 | ### pip |
| 199 | ```yaml |
| 200 | - uses: actions/cache@v4 |
| 201 | with: |
| 202 | path: ~/.cache/pip |
| 203 | key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }} |
| 204 | ``` |
| 205 | |
| 206 | ### Docker Layer Caching |
| 207 | ```yaml |
| 208 | - uses: docker/build-push-action@v5 |
| 209 | with: |
| 210 | cache-from: type=gha |
| 211 | cache-to: type=gha,mode=max |
| 212 | ``` |
| 213 | |
| 214 | ## Kubernetes Deployment |
| 215 | |
| 216 | ### Rolling Update |
| 217 | ```yaml |
| 218 | apiVersion: apps/v1 |
| 219 | kind: Deployment |
| 220 | metadata: |
| 221 | name: api |
| 222 | spec: |
| 223 | replicas: 3 |
| 224 | strategy: |
| 225 | type: RollingUpdate |
| 226 | rollingUpdate: |
| 227 | maxSurge: 1 |
| 228 | maxUnavailable: 0 |
| 229 | template: |
| 230 | spec: |
| 231 | containers: |
| 232 | - name: api |
| 233 | image: ghcr.io/org/api:latest |
| 234 | readinessProbe: |
| 235 | httpGet: |
| 236 | path: /ready |
| 237 | port: 3000 |
| 238 | initialDelaySeconds: 5 |
| 239 | livenessProbe: |
| 240 | httpGet: |
| 241 | path: /live |
| 242 | port: 3000 |
| 243 | initialDelaySeconds: 10 |
| 244 | ``` |
| 245 | |
| 246 | ## S |