iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0
生成式 AI

從系統設計切入,探索 GenAI 在企業中的實踐系列 第 14

[Day14] 加速開發:CI/CD 實踐自動化部署策略

  • 分享至 

  • xImage
  •  

當我完成我的第一個專案,準備進入到第二個專案時,由於過去的經驗我特別著重在想要學好 CI/CD,不僅能脫繁瑣的手動部署與測試等重複性工作,還能專注於解決技術挑戰和開發創新功能。尤其在生成式 AI 領域,CI/CD 能讓團隊迅速適應模型、框架和技術的快速迭代,使替換新工具或新流程變得更加簡單。

CI/CD 運作架構

https://ithelp.ithome.com.tw/upload/images/20240912/20151660ZvPIwj0GQm.jpg

這是第一次架設 CI/CD pipeline 所參考的架構圖,當時是使用 Azure 結合 GCP 的方式執行,而這張圖是純粹 Azure 架構,呈現了完整的流程:

  1. 開發者工作流程:統一將程式碼提交到程式碼倉庫(Repositories),並透過 Git 控管。
  2. Azure Pipelines 三階段流程:
    • PR (Pull Request) 階段:程式碼分析、lint (風格)檢查、安全掃描、映像建構和單元測試等。
    • CI (Continuous Integration) 階段:獲取密鑰、程式碼分析、映像構建、單元和集成測試,發佈映像。
    • CD (Continuous Deployment) 階段:下載映像並部署到測試環境、驗收測試、人工干預,發布版本。
  3. 資源管理:
    • Azure Artifacts 用於儲存映像檔。
    • Azure Key Vault 用於管理密鑰和機敏資訊。
  4. 部署環境:
    • Staging(測試)環境:包括虛擬機、主程式和儀錶板。
    • Production(正式產品)環境:同測試環境,供用戶使用。
  5. 監控和分析:
    • Azure Monitor 用於監控服務性能。
    • Application Insights 用於用戶洞察。
    • Log Analytics Workspace 用於執行日誌分析。

從 YAML 腳本看 CI/CD 運作邏輯

以上圖為例,以下是只部署至測試環境,通常會讓不同的分支綁定不同環境,如 main 分支綁定 Production、develop 分支綁定 Staging,並設定在該分支執行特定事件時觸發 CI/CD。

# 工作流程名稱
name: GenAI Azure CI/CD Pipeline to Staging

# 定義觸發工作流程的事件
on:
  push:
    branches: [develop]
  pull_request:
    branches: [develop]

# 定義環境變量
env:
  AZURE_WEBAPP_NAME: your-staging-webapp-name 
  AZURE_RESOURCE_GROUP: your-resource-group

# 分別會執行 pr_checks, build_and_test, deploy_to_staging, setup_monitoring 四個 jobs
jobs:

  pr_checks:
    runs-on: ubuntu-latest  # job 執行環境
    steps:  # job 執行步驟
    - uses: actions/checkout@v3
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Code analysis
      run: |
        pip install pylint
        pylint **/*.py
    - name: Lint check
      run: |
        pip install flake8
        flake8 .
    - name: Security scan
      run: |
        pip install bandit
        bandit -r . -f custom
    - name: Run unit tests
      run: |
        pip install pytest
        pytest tests/unit

  # CI (Continuous Integration) 階段:建構映像並測試
  build_and_test:
    needs: pr_checks
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Azure Key Vault - Get Secrets
      uses: Azure/get-keyvault-secrets@v1
      with:
        keyvault: "your-keyvault-name"
        secrets: 'secret1, secret2'  # 列出需要的 keys
      id: myGetSecretAction
    - name: Build and push image to ACR  # 將建立好的映像推送至儲存庫
      uses: azure/docker-login@v1
      with:
        login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}
    - run: |
        docker build . -t ${{ secrets.REGISTRY_LOGIN_SERVER }}/genaiapp:${{ github.sha }}
        docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/genaiapp:${{ github.sha }}
    - name: Run integration tests  # 執行集成測試
      run: |
        # 執行集成測試的程式碼
        echo "Running integration tests..."
    - name: Upload artifact to Azure Pipelines
      uses: actions/upload-artifact@v2
      with:
        name: drop
        path: ${{ github.workspace }}

  # CD (Continuous Deployment) 階段:下載映像檔並部署至測試環境
  deploy_to_staging:
    needs: build_and_test
    runs-on: ubuntu-latest
    steps:
    - name: Download artifact from Azure Pipelines
      uses: actions/download-artifact@v2
      with:
        name: drop
        path: ${{ github.workspace }}
    - name: Deploy to Azure Web App for Staging
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE  }}
        images: '${{ secrets.REGISTRY_LOGIN_SERVER }}/genaiapp:${{ github.sha }}'
    - name: Run acceptance tests
      run: |
        # 執行驗收測試的程式碼
        echo "Running acceptance tests on Staging environment..."

  # 設置監控與分析
  setup_monitoring:
    needs: deploy_to_staging
    runs-on: ubuntu-latest
    steps:
    - name: Setup Azure Monitor
      uses: Azure/cli@v1.0.0
      with:
        inlineScript: |
          az monitor app-insights component create --app GenAIMonitor-Staging --location eastus --kind web --resource-group ${{ env.AZURE_RESOURCE_GROUP }}
    - name: Setup Application Insights
      run: |
        # 設置 Application Insights 的程式碼
        echo "Setting up Application Insights for Staging..."
    - name: Configure Log Analytics
      run: |
        # 設置 Log Analytics Workspace 的程式碼
        echo "Configuring Log Analytics Workspace for Staging..."

上一篇
[Day13] 從單打獨鬥到團隊協作:Git 流程規範制定
下一篇
[Day15] 掌握設計模式開發核心組件-單例、工廠模式
系列文
從系統設計切入,探索 GenAI 在企業中的實踐27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言