Chuyển đến nội dung chính

第 24 課:微服務和微前端的 CI/CD 管道

設計端對端 CI/CD 管道。用於建置/測試的 GitHub Actions。 Docker 多階段建置。容器註冊表(ECR/GHCR)。用於 Kubernetes 部署的 ArgoCD GitOps。多個服務的並行管道。

🏗️ 建築 — 第 24 課 第 24 課:微服務的 CI/CD 管道 & 微前端

微服務與微前端系統設計-從基礎到生產

第 8 部分:CI/CD 和部署策略

亞洲開發網

簡介

CI/CD Pipeline 擁有 10 多個微服務和 5 個以上微前端,是獨立部署的骨幹。本文設計了一個端到端的管道:從提交到生產。

CI/CD Pipeline cho Microservices + Micro Frontend


1. CI/CD 架構

┌─────────────────────────────────────────────────┐
│ Developer pushes code                           │
└──────────────────────┬──────────────────────────┘
                       │
┌──────────────────────┴──────────────────────────┐
│ CI Pipeline (GitHub Actions)                    │
│ ┌─────────┐ ┌──────────┐ ┌───────────────────┐ │
│ │  Lint   │ │  Build   │ │  Test             │ │
│ │  & Type │→│  & Compile│→│  Unit + Integration│ │
│ │  Check  │ │          │ │  + Contract       │ │
│ └─────────┘ └──────────┘ └───────────────────┘ │
│                                    │            │
│                     ┌──────────────┴──────────┐ │
│                     │ Docker Build & Push     │ │
│                     │ → Container Registry    │ │
│                     └──────────────┬──────────┘ │
└────────────────────────────────────┼────────────┘
                                     │
┌────────────────────────────────────┴────────────┐
│ CD Pipeline (ArgoCD - GitOps)                   │
│                                                 │
│ ┌──────────┐  ┌──────────┐  ┌────────────────┐ │
│ │ Staging  │→ │  Smoke   │→ │  Production    │ │
│ │ Deploy   │  │  Tests   │  │  Deploy        │ │
│ │          │  │          │  │  (Progressive) │ │
│ └──────────┘  └──────────┘  └────────────────┘ │
└─────────────────────────────────────────────────┘

2. CI 管道(GitHub Actions)

2.1 微服務管道

# .github/workflows/service-ci.yml
name: Service CI

on:
  push:
    paths:
      - 'services/product-service/**'
      - 'packages/shared-types/**'

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Lint & Type Check
        run: turbo lint typecheck --filter=product-service

      - name: Unit Tests
        run: turbo test --filter=product-service

      - name: Integration Tests
        run: turbo test:integration --filter=product-service
        services:
          postgres:
            image: postgres:16
            env:
              POSTGRES_DB: test

      - name: Contract Verification
        run: turbo test:pact --filter=product-service

      - name: Docker Build & Push
        run: |
          docker build -t ghcr.io/company/product-service:${{ github.sha }} \
            -f services/product-service/Dockerfile .
          docker push ghcr.io/company/product-service:${{ github.sha }}

2.2 微前端管道

# .github/workflows/mfe-ci.yml
name: MFE CI

on:
  push:
    paths:
      - 'apps/product-mfe/**'
      - 'packages/shared-ui/**'

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install & Build
        run: turbo build --filter=product-mfe...

      - name: Test
        run: turbo test --filter=product-mfe

      - name: Visual Regression
        uses: chromatic/action@v1
        with:
          projectToken: ${{ secrets.CHROMATIC_TOKEN }}

      - name: Deploy to CDN
        run: |
          aws s3 sync apps/product-mfe/dist/ \
            s3://mfe-cdn/product-mfe/${{ github.sha }}/
          aws cloudfront create-invalidation \
            --distribution-id $CF_ID \
            --paths "/product-mfe/*"

3. Docker 多階段構建

# services/product-service/Dockerfile
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --production=false
COPY . .
RUN npm run build

# Stage 2: Production
FROM node:20-alpine AS runner
WORKDIR /app
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
USER appuser
EXPOSE 8080
CMD ["node", "dist/main.js"]

4.GitOps 與 ArgoCD

4.1 GitOps 工作流程

Developer pushes code → CI builds image → CI updates manifests repo
                                                    │
ArgoCD watches manifests repo ──────────────────────┘
ArgoCD syncs K8s cluster to match manifests

4.2 自訂疊加

# k8s/overlays/production/product-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-service
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: product-service
          image: ghcr.io/company/product-service:abc123
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
            limits:
              cpu: 500m
              memory: 512Mi

5. 平行管道

Commit changes to:
├── services/product-service/ → Product Service Pipeline
├── apps/product-mfe/         → Product MFE Pipeline
└── packages/shared-ui/       → Shared UI Pipeline
                                  ↓
                              Triggers dependent:
                              → Product MFE Pipeline
                              → Cart MFE Pipeline
                              → All MFE consumers

渦輪雷波 --filter=...[origin/main] 自動偵測受影響的包。


6. 管道最佳實踐

實踐描述
快速失敗Lint → 單元 → 整合(首先需要一點時間)
並行工作Lint + 並行測試運行
快取npm 快取、Docker 層快取、Turborepo 遠端快取
僅受影響僅建置/測試包變更
不可變的工件使用 git SHA 標記影像
GitOps單獨的設定儲存庫,ArgoCD 同步

總結

  • CI:Lint → 建置 → 測試 → Docker → 推送(每個服務/MFE)
  • CD:帶有 ArgoCD 的 GitOps,聲明性 K8s 清單
  • Turborepo:影響檢測,僅更改建置包
  • Docker 多階段:小型、安全的生產映像
  • 並行管道:每個服務/MFE都有自己的管道

下一篇文章: 第 25 課:部署策略 — 藍/綠、金絲雀與滾動