使用Devops CI/CD将Django Web App部署到Azure App Service

使用Devops CI/CD将Django Web App部署到Azure App Service

问题描述:

我正在尝试使用CI/CD管道(Microsoft为应用程序部署提供的最基本的管道,将简单的django Web ap部署到Azure App Service),我没有做任何更改).但是我收到以下错误:

I'm trying to deploy simple django web ap to Azure App Service using CI/CD pipeline (the most basic one that is offered by Microsoft for app deployment- no changes from me). However I'm getting the following error:

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'

我检查了其他线程,并尝试完成所提到的所有事情,但没有帮助,或者我错过了一些事情:

I checked other threads and tried doing all the things mentioned but it did not help, or I am missing something:

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)

But still getting the same error, where django is not recognized. I can see that reuqirements.txt is being installed successfully and it has all the neccessary libraries there (including Django)

我的CI/CD yaml文件如下:

My CI/CD yaml file looks like this:

# 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 App Service中配置某些内容?但是我不确定到底是什么.

Maybe I need to configure something in the Azure App Service? But i am not sure exactly what.

我之前遇到过此问题,而问题可能出在您的部署方法上.不确定您使用的是哪个,但是下面的经典部署中心已被弃用,请尝试使用新的部署中心.与我一起工作,检查了您的工作流程,没有什么不同.因此,我将张贴在我这一边的正确步骤供您参考.

I have met this issue before, and the problem might be your deployment method. Not sure which one you use, but the classic deployment center below is being deprecated, try use the new deployment center. Checked your workflow with the one worked on my side, there is nothing different. So I will post the correct step worked on my side for you to refer.

  1. 在本地检查您的项目,以确保其可以成功运行.

  1. check your project locally to make sure it could run successfully.

创建一个网络应用程序(以确保不会损坏您的网络应用程序),并导航至部署中心页面.

Create a new web app (this is to make sure no damage on your web app) and navigate to the Deployment center page.

转到您的GitHub并导航到 GitHub Action 页面以查看日志.

Go to your GitHub and navigate to GitHub Action page to see the log.

测试您的Web应用程序并检查KuDu网站上的文件结构: https://{yourappname} .scm.azurewebsites.net/wwwroot/

Test your web app and check the file structure on KuDu site: https://{yourappname}.scm.azurewebsites.net/wwwroot/

您可以像我一样单击浏览按钮进行测试.如果要运行命令,请转到以下站点: https://{yourappname} .scm.azurewebsites.net/DebugConsole

You could test by click the browse button like what I did. If you want to run command, go to this site: https://{yourappname}.scm.azurewebsites.net/DebugConsole

如果需要,我会发布此链接>使用DevOps部署.

By the way, I post this link if you need deploy using DevOps.