Linux 上的 Azure AppService 和 Django 使用 Azure Devops for CI 需要 20 分钟

问题描述 投票:0回答:1

我在 Linux 上使用 Django 和 Azure Devops 作为持续集成/持续开发的目的。

我已成功设置用于部署的 yaml 文件并与 Azure AppService 关联,该文件似乎工作正常。

但是需要很长的时间(大约20分钟),其他云提供商的速度要快得多(比如3-4分钟)

下面是我的 azure-pipelines-prod.yaml 文件:

# Python to Linux Web App on Azure
# Build your Python project and deploy it to Azure as a Linux Web App.
# Change python version to one thats appropriate for your application.
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- main

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: 'xxxxxxxx-7f93-4dc6-922b-ae9b29311fbf'

  # Web app name
  webAppName: 'project-app'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Environment name
  environmentName: 'project-app'

  # Project root folder. Point to the folder containing manage.py file.
  projectRoot: $(System.DefaultWorkingDirectory)

  pythonVersion: '3.9'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(pythonVersion)'
      displayName: 'Use Python $(pythonVersion)'

    - script: |
        python -m venv antenv
        source antenv/bin/activate
        python -m pip install --upgrade pip
        pip install setup
        pip install --target="./antenv/lib/python3.9/site-packages" -r ./requirements-azure.txt
      workingDirectory: $(projectRoot)
      displayName: "Install requirements"

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(projectRoot)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      displayName: 'Upload package'
      artifact: drop

- stage: Deploy
  displayName: 'Deploy Web App'
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: DeploymentJob
    pool:
      vmImage: $(vmImageName)
    environment: $(environmentName)
    strategy:
      runOnce:
        deploy:
          steps:

          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
            displayName: 'Use Python version'

          - task: AzureWebApp@1
            displayName: 'Deploy Azure Web App : project-app'
            inputs:
              azureSubscription: $(azureServiceConnectionId)
              appName: $(webAppName)
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

下面是我的 AppService 环境设置:

[
  {
    "name": "AZURE_CLIENT_ID",
    "value": "xxxxxxx-790e-4de9-9efe-60f046024752",
    "slotSetting": true
  },
  {
    "name": "AZURE_CLIENT_SECRET",
    "value": "XXXXXXXXX~9ezzAV2qI5uDVx8Khc7CMCDXf5mfICFbQN",
    "slotSetting": true
  },
  {
    "name": "DiagnosticServices_EXTENSION_VERSION",
    "value": "disabled",
    "slotSetting": true
  },
  {
    "name": "DISABLE_PHP_BUILD",
    "value": "false",
    "slotSetting": false
  },
  {
    "name": "InstrumentationEngine_EXTENSION_VERSION",
    "value": "disabled",
    "slotSetting": true
  },
  {
    "name": "PORT",
    "value": "8000",
    "slotSetting": true
  },
  {
    "name": "SnapshotDebugger_EXTENSION_VERSION",
    "value": "disabled",
    "slotSetting": true
  },
  {
    "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS",
    "value": "3",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_WEBDEPLOY_USE_SCM",
    "value": "true",
    "slotSetting": true
  },
  {
    "name": "WEBSITES_CONTAINER_START_TIME_LIMIT",
    "value": "1000",
    "slotSetting": false
  },
  {
    "name": "WEBSITES_PORT",
    "value": "8000",
    "slotSetting": true
  },
  {
    "name": "XDT_MicrosoftApplicationInsights_BaseExtensions",
    "value": "disabled",
    "slotSetting": true
  },
  {
    "name": "XDT_MicrosoftApplicationInsights_Mode",
    "value": "default",
    "slotSetting": true
  },
  {
    "name": "XDT_MicrosoftApplicationInsights_PreemptSdk",
    "value": "disabled",
    "slotSetting": true
  }
]

每当我提交到 azure-pipeline-prod 中定义的主分支时,它都会触发构建,然后构建 zip 会加载到 AppService 上。然而,它确实非常慢,并且每月消耗 Azure 订阅授予的 1800 分钟并行构建时间。 zip 文件大约 1 MB,包含所有 django 包的工件大约 170 MB,如下所示:

Physical Content Uploaded: 147.7 MB
Logical Content Uploaded: 154.9 MB
Compression Saved: 7.2 MB
Deduplication Saved: 181.2 MB
Number of Chunks Uploaded: 127
Total Number of Chunks: 409

我愿意接受任何建议,以便使其更稳定(构建有时会失败)并且更快。

更新:

构建/部署过程停留在以下步骤并记录大约 16 分钟:

部署 Azure Web 应用程序:项目应用程序

##[debug][GET]https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z
##[debug]POLL URL RESULT: {"statusCode":202,"statusMessage":"Accepted","headers":{"content-length":"825","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:08:44 GMT","server":"Kestrel","location":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":1,"status_text":"Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"Running deployment command...","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":null,"last_success_end_time":null,"complete":false,"active":false,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]Deployment status: 1 'Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.'. retry after 5 seconds
##[debug]Agent environment resources - Disk: / Available 22269.41 MB out of 74244.74 MB, Memory: Used 1120.00 MB out of 6930.00 MB, CPU: Usage 1.48%
##[debug]setting affinity cookie ["ARRAffinity=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net","ARRAffinitySameSite=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;SameSite=None;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net"]
##[debug]CLIENT_RESETSTREAMONRETRY=undefined
##[debug][GET]https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z
##[debug]POLL URL RESULT: {"statusCode":202,"statusMessage":"Accepted","headers":{"content-length":"825","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:08:50 GMT","server":"Kestrel","location":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":1,"status_text":"Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"Running deployment command...","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":null,"last_success_end_time":null,"complete":false,"active":false,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]Deployment status: 1 'Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.'. retry after 5 seconds
##[debug]Agent environment resources - Disk: / Available 22269.41 MB out of 74244.74 MB, Memory: Used 1120.00 MB out of 6930.00 MB, CPU: Usage 0.48%
##[debug]setting affinity cookie ["ARRAffinity=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net","ARRAffinitySameSite=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;SameSite=None;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net"]
##[debug]CLIENT_RESETSTREAMONRETRY=undefined
##[debug][GET]https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z
##[debug]POLL URL RESULT: {"statusCode":202,"statusMessage":"Accepted","headers":{"content-length":"825","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:08:56 GMT","server":"Kestrel","location":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":1,"status_text":"Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"Running deployment command...","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":null,"last_success_end_time":null,"complete":false,"active":false,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]Deployment status: 1 'Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.'. retry after 5 seconds
##[debug]Agent environment resources - Disk: / Available 22269.41 MB out of 74244.74 MB, Memory: Used 1120.00 MB out of 6930.00 MB, CPU: Usage 0.95%
##[debug]setting affinity cookie ["ARRAffinity=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net","ARRAffinitySameSite=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;SameSite=None;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net"]
##[debug]CLIENT_RESETSTREAMONRETRY=undefined
##[debug][GET]https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z
##[debug]Agent environment resources - Disk: / Available 22269.41 MB out of 74244.74 MB, Memory: Used 1120.00 MB out of 6930.00 MB, CPU: Usage 13.98%
##[debug]POLL URL RESULT: {"statusCode":202,"statusMessage":"Accepted","headers":{"content-length":"825","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:09:01 GMT","server":"Kestrel","location":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":1,"status_text":"Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"Running deployment command...","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":null,"last_success_end_time":null,"complete":false,"active":false,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]Deployment status: 1 'Building and Deploying '76d0215b-28d3-4460-ad39-c22b2b45dbd2'.'. retry after 5 seconds
##[debug]setting affinity cookie ["ARRAffinity=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net","ARRAffinitySameSite=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;SameSite=None;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net"]
##[debug]CLIENT_RESETSTREAMONRETRY=undefined
##[debug][GET]https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net:443/api/deployments/latest?deployer=VSTS_ZIP_DEPLOY&time=2024-09-21_13-51-54Z
##[debug]POLL URL RESULT: {"statusCode":200,"statusMessage":"OK","headers":{"content-length":"784","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:09:07 GMT","server":"Kestrel"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":4,"status_text":"","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":"2024-09-21T14:09:04.7067259Z","last_success_end_time":"2024-09-21T14:09:04.7067259Z","complete":true,"active":true,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]setting affinity cookie ["ARRAffinity=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net","ARRAffinitySameSite=4171aae28232215c7c1d6de5a1f7b3cadffd5db6661afdb90cc5cdd8c510f819;Path=/;HttpOnly;SameSite=None;Secure;Domain=videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net"]
##[debug]CLIENT_RESETSTREAMONRETRY=undefined
##[debug][GET]https://***:***@videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2
##[debug]getDeploymentDetails. Data: {"statusCode":200,"statusMessage":"OK","headers":{"content-length":"784","content-type":"application/json; charset=utf-8","date":"Sat, 21 Sep 2024 14:09:07 GMT","server":"Kestrel"},"body":{"id":"76d0215b-28d3-4460-ad39-c22b2b45dbd2","status":4,"status_text":"","author_email":"N/A","author":"N/A","deployer":"VSTS_ZIP_DEPLOY","message":"Created via a push deployment","progress":"","received_time":"2024-09-21T13:52:11.1812709Z","start_time":"2024-09-21T13:52:12.4271044Z","end_time":"2024-09-21T14:09:04.7067259Z","last_success_end_time":"2024-09-21T14:09:04.7067259Z","complete":true,"active":true,"is_temp":false,"is_readonly":true,"url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2","log_url":"https://videoo-app-g3esbuddfgcfhjgg.scm.uksouth-01.azurewebsites.net/api/deployments/76d0215b-28d3-4460-ad39-c22b2b45dbd2/log","site_name":"videoo-app","build_summary":{"errors":[],"warnings":[]}}}
##[debug]system.debug=True

azure devops for django appservice deployment logs

django azure azure-devops azure-pipelines azure-web-app-service
1个回答
0
投票

根据文档直接从 ZIP 包在 Azure 应用服务中运行应用程序启用构建自动化

Python 应用程序不支持从包运行。部署 Python 代码的 ZIP 文件时,需要设置一个标志以启用 Azure 构建自动化。构建自动化将为您的应用程序创建 Python 虚拟环境并安装任何必要的要求和所需的包。

部署 Python 代码的 ZIP 文件时,需要设置一个标志以启用 Azure 构建自动化。构建自动化将安装任何必要的要求并将应用程序打包以在 Azure 上运行。

通过在 Azure 门户或 Azure CLI 中设置

SCM_DO_BUILD_DURING_DEPLOYMENT
应用程序设置来启用 Azure 中的构建自动化。

Image

据我测试,我的部署失败了,通过

az webapp deploy --name $APP_SERVICE_NAME --resource-group $RESOURCE_GROUP_NAME --type zip --src-path "local.zip"
命令和管道部署遇到了类似的问题,需要比平时更多的时间,直到 我在应用程序设置中将环境变量
SCM_DO_BUILD_DURING_DEPLOYMENT
设置为
true

要验证并缩小/排除问题的可能根本原因,您还可以通过 Azure CloudShell 或本地 PowerShell 运行 Azure CLI 命令

az webapp deploy --name $APP_SERVICE_NAME --resource-group $RESOURCE_GROUP_NAME --type zip --src-path "local.zip"
来部署相同的项目 zip 文件,然后再将部署集成到管道中。

© www.soinside.com 2019 - 2024. All rights reserved.