🔄 CI vs CD

Continuous Integration vs Delivery/Deployment — Pipeline tự động hóa

🟣 CI (Continuous Integration)

Tự động build + test mỗi khi push code. Phát hiện lỗi sớm, merge thường xuyên.

🟢 CD (Continuous Delivery/Deployment)

Tự động deploy lên staging/production. Delivery = manual approve. Deployment = full auto.

1. CI/CD Pipeline

Full CI/CD Pipeline:

  Push Code → CI: Build → CI: Test → CD: Deploy Staging → CD: Deploy Prod
  ─────────   ──────────   ─────────   ─────────────────   ────────────────
      │            │           │              │                    │
   git push    compile      unit test     auto deploy        auto deploy
   to main     lint         integration   to staging         to production
               build        e2e test      ↓                  ↓
                                          Manual approve?    ← CD Delivery
                                          OR                 ← CD Deployment
                                          Auto (full auto)

2. CI (Continuous Integration)

Mỗi khi developer push code → tự động chạy:

  • Build: Compile, linting, type checking
  • Unit Tests: Test từng function/module
  • Integration Tests: Test các modules tương tác
  • Code Quality: SonarQube, ESLint, Coverage
# GitHub Actions — CI Pipeline
name: CI
on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Type check
        run: npm run type-check

      - name: Unit tests
        run: npm test -- --coverage

      - name: Build
        run: npm run build

3. CD (Continuous Delivery vs Deployment)

⚠️ 2 loại CD:

Continuous Delivery: Auto deploy đến staging → cần manual approve để deploy production.

Continuous Deployment: Auto deploy luôn lên production. Mọi commit pass tests → production ngay.

# GitHub Actions — CD Pipeline
name: CD
on:
  push:
    branches: [main]

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    needs: build-and-test  # CI phải pass trước
    steps:
      - name: Deploy to Staging
        run: |
          ssh deploy@staging "cd /app && git pull && docker-compose up -d"

  deploy-production:
    runs-on: ubuntu-latest
    needs: deploy-staging
    environment: production  # ← Cần manual approve (Continuous Delivery)
    # Bỏ environment → auto deploy (Continuous Deployment)
    steps:
      - name: Deploy to Production
        run: |
          ssh deploy@prod "cd /app && git pull && docker-compose up -d"

      - name: Health check
        run: curl -f https://myapp.com/health || exit 1

4. Bảng So Sánh

Tiêu chí 🟣 CI 🟢 CD (Delivery) 🟢 CD (Deployment)
Mục đích Build + Test tự động Deploy staging auto Deploy prod auto
Trigger Push/PR CI pass CI pass
Manual step? Không Approve prod deploy Không
Risk Thấp Trung bình Cao (cần tests tốt)
Speed Nhanh Nhanh + gate Nhanh nhất
Dùng bởi Mọi team Enterprise Netflix, Facebook

5. Tools Phổ Biến

CI/CD Tools:
GitHub Actions — tích hợp GitHub, YAML config
GitLab CI/CD — tích hợp GitLab, .gitlab-ci.yml
Jenkins — self-hosted, plugin ecosystem
CircleCI — cloud-based, config as code
ArgoCD — GitOps cho Kubernetes