EngineeringSolution ArchitectAzure Playbooks

Azure App Service – Playbook Triển Khai .NET / Next.js

Kịch bản

Triển khai một ứng dụng Next.js 14+ (hoặc ASP.NET Core) lên Azure App Service sử dụng GitHub Actions cho CI/CD, với slot swap không gây gián đoạn và định danh miền tùy chỉnh.

Yêu cầu trước

  • Azure subscription.
  • Resource Group (rg-wiki).
  • App Service Plan (Standard S1 hoặc Premium P1) – hỗ trợ Linux containers.
  • Azure Web App (wiki-app).
  • Repository GitHub chứa mã nguồn wiki.
  • (Tùy chọn) miền tùy chỉnh (ví dụ: wiki.mycompany.com).

Các bước thực hiện

1. Tạo App Service (CLI)

bash
az group create -n rg-wiki -l eastus
az appservice plan create \
    --name plan-wiki \
    --resource-group rg-wiki \
    --sku P1V2 \
    --is-linux
az webapp create \
    --resource-group rg-wiki \
    --plan plan-wiki \
    --name wiki-app \
    --runtime "NODE|18-lts"   # cho Next.js, hoặc "DOTNET|8.0" cho .NET

2. Kích hoạt Deployment Slots (staging)

bash
az webapp deployment slot create \
    --resource-group rg-wiki \
    --name wiki-app \
    --slot staging

Slot staging sẽ nhận bản build mới; sau khi kiểm tra, chúng ta swap với production.

3. Cấu hình App Settings (biến môi trường)

bash
az webapp config appsettings set \
    --resource-group rg-wiki \
    --name wiki-app \
    --settings NODE_ENV=production
# Thêm secret qua Key Vault reference (xem Playbook Key Vault)

4. Thiết lập GitHub Actions workflow (.github/workflows/deploy.yml)

yaml
name: Deploy Wiki to Azure App Service

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Node
        uses: actions/setup-node@v3
        with:
          node-version: '18'
      - name: Install dependencies
        run: npm ci
      - name: Build Next.js
        run: npm run build
      - name: Deploy to Azure (staging slot)
        uses: azure/webapps-deploy@v2
        with:
          app-name: wiki-app
          slot-name: staging
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          package: ./.next
      - name: Swap slots
        if: success()
        run: |
          az webapp deployment slot swap \
            --resource-group rg-wiki \
            --name wiki-app \
            --slot staging \
            --target-slot production

Lưu ý: Lưu publish profile (*.publishsettings) dưới dạng secret AZURE_WEBAPP_PUBLISH_PROFILE.

5. Domain & TLS (tùy chọn)

bash
az webapp config hostname add \
    --resource-group rg-wiki \
    --webapp-name wiki-app \
    --hostname wiki.mycompany.com
az webapp config ssl bind \
    --resource-group rg-wiki \
    --name wiki-app \
    --certificate-thumbprint <thumbprint> \
    --ssl-type SNI

Bạn có thể lấy chứng chỉ miễn phí từ Let's Encrypt bằng tính năng App Service Managed Certificate.

6. Giám sát & Cảnh báo

  • Kích hoạt Application Insights (liên kết qua az webapp config appsettings set --settings APPLICATIONINSIGHTS_CONNECTION_STRING=...).
  • Thiết lập Azure Monitor alerts cho Http5xx, CpuPercentage > 80%, MemoryPercentage > 80%.

Mẹo thực hành tốt

  • Slot Warm‑up: Thêm endpoint warmup (/api/health) và bật auto‑swap sau khi health check thành công.
  • Zero‑downtime: Giữ slot production luôn phục vụ traffic; chỉ swap sau khi staging đã qua kiểm tra.
  • Secrets: Không lưu secret trong file workflow; tham chiếu Key Vault (xem Playbook Key Vault).
  • Scaling: Sử dụng App Service Autoscale dựa trên CPU hoặc số request cho các đợt traffic tăng cao.
  • CDN cho static assets: Kích hoạt Azure Front Door hoặc Azure CDN để cache các file tĩnh (/_next/static/*).

File thuộc mục Azure Playbooks của senior‑dotnet‑wiki.