使用 Devops CI/CD 将 Django Web 应用部署到 Azure 应用服务

发布时间:2021-03-08 17:23

我正在尝试使用 CI/CD 管道(Microsoft 为应用程序部署提供的最基本的管道 - 我没有更改)将简单的 django web ap 部署到 Azure 应用服务。但是我收到以下错误:

2021-03-08T16:55:51.172914117Z   File "", line 219, in _call_with_frames_removed
2021-03-08T16:55:51.172918317Z   File "/home/site/wwwroot/deytabank_auth/wsgi.py", line 13, in 
2021-03-08T16:55:51.172923117Z     from django.core.wsgi import get_wsgi_application
2021-03-08T16:55:51.172927017Z ModuleNotFoundError: No module named 'django'

我检查了其他线程并尝试做所有提到的事情,但没有帮助,或者我遗漏了一些东西:

wsgi.py 中,我添加了:

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../licenses_api')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../deytabank_auth')

from django.core.wsgi import get_wsgi_application


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deytabank_auth.settings')

application = get_wsgi_application()

但仍然出现相同的错误,无法识别 django。我可以看到 reuqirements.txt 已成功安装,其中包含所有必需的库(包括 Django)

我的 CI/CD 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:
- develop

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: '***'

  # Web app name
  webAppName: 'DeytabankAuth'

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

  # Environment name
  environmentName: 'DeytabankAuth'

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

  # Python version: 3.7
  pythonVersion: '3.7'

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 -r requirements.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 : DeytabankAuth'
            inputs:
              azureSubscription: $(azureServiceConnectionId)
              appName: $(webAppName)
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

也许我需要在 Azure 应用服务中配置一些东西?但我不确定到底是什么。

回答1

我之前遇到过这个问题,问题可能是你的部署方式。不确定您使用的是哪一个,但下面的经典部署中心已被弃用,请尝试使用新的部署中心。 enter image description here 用在我这边工作的人检查了你的工作流程,没有什么不同。所以我会把我这边的正确步骤贴出来供大家参考。

  1. 检查您的项目本地以确保它可以成功运行。

  2. 创建一个网络应用程序(这是为了确保您的网络应用程序没有损坏)并导航到部署中心页面。 enter image description here

  3. 转到您的 GitHub 并导航到 GitHub 操作 页面以查看日志。 enter image description here

  4. 测试您的网络应用并检查 KuDu 网站上的文件结构:https://{yourappname}.scm.azurewebsites.net/wwwroot/ enter image description here

您可以像我一样通过单击浏览按钮进行测试。 enter image description here 如果要运行命令,请访问此站点:https://{yourappname}.scm.azurewebsites.net/DebugConsole enter image description here


顺便说一下,如果您需要使用 DevOps 进行部署,我会发布此link

回答2

这个问题的可能原因是你没有安装 Django。

在 Microsoft 托管的代理 ubuntu-latest 中,未预装 Django。也就是说,您需要手动安装它。

pip install Django==3.1.7

单击 this document 以获取有关下载 Django 的详细信息。