在Azure Web App上缩短Django项目的部署时间

t40tm48m  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(85)

我在Azure Web App(Linux,P2 v2,Python 3.10,Django 4.2)上部署了一个Django项目,该项目按预期运行。但是,部署时间非常慢,即使是代码中的一个小更改也需要15到20分钟才能完成。这比我在其他服务上经历的部署时间要长得多。
下面是部署日志的一个片段:

2023-10-31T12:50:59.953Z - Updating branch 'master'.
2023-10-31T12:51:04.983Z - Updating submodules.
2023-10-31T12:51:05.086Z - Preparing deployment for commit id 'XXXXXXXXX'.
2023-10-31T12:51:05.338Z - PreDeployment: context.CleanOutputPath False
2023-10-31T12:51:05.432Z - PreDeployment: context.OutputPath /home/site/wwwroot
2023-10-31T12:51:05.535Z - Repository path is /home/site/repository
2023-10-31T12:51:05.638Z - Running oryx build...
2023-10-31T12:51:05.644Z - Command: oryx build /home/site/repository -o /home/site/wwwroot --platform python --platform-version 3.10 -p virtualenv_name=antenv --log-file /tmp/build-debug.log  -i /tmp/8dbda100b9921ec --compress-destination-dir | tee /tmp/oryx-build.log
2023-10-31T12:51:06.599Z - Operation performed by Microsoft Oryx, https://github.com/Microsoft/Oryx
2023-10-31T12:51:06.614Z - You can report issues at https://github.com/Microsoft/Oryx/issues
2023-10-31T12:51:06.643Z - Oryx Version: 0.2.20230508.1, Commit: 7fe2bf39b357dd68572b438a85ca50b5ecfb4592, ReleaseTagName: 20230508.1
2023-10-31T12:51:06.658Z - Build Operation ID: 10aafab84d551a74
2023-10-31T12:51:06.664Z - Repository Commit : XXXXXXXXXXXXXX
2023-10-31T12:51:06.670Z - OS Type           : bullseye
2023-10-31T12:51:06.677Z - Image Type        : githubactions
2023-10-31T12:51:06.695Z - Detecting platforms...
2023-10-31T12:51:15.575Z - Detected following platforms:
2023-10-31T12:51:15.608Z -   nodejs: 16.20.2
2023-10-31T12:51:15.613Z -   python: 3.10.8
2023-10-31T12:51:15.618Z - Version '16.20.2' of platform 'nodejs' is not installed. Generating script to install it...
2023-10-31T12:51:15.623Z - Version '3.10.8' of platform 'python' is not installed. Generating script to install it...
2023-10-31T12:51:15.811Z - Using intermediate directory '/tmp/8dbda100b9921ec'.
2023-10-31T12:51:15.837Z - Copying files to the intermediate directory...
2023-10-31T12:51:17.447Z - Done in 2 sec(s).
2023-10-31T12:51:17.465Z - Source directory     : /tmp/8dbda100b9921ec
2023-10-31T12:51:17.470Z - Destination directory: /home/site/wwwroot
2023-10-31T12:51:17.487Z - Downloading and extracting 'nodejs' version '16.20.2' to '/tmp/oryx/platforms/nodejs/16.20.2'...
2023-10-31T12:51:17.496Z - Detected image debian flavor: bullseye.
2023-10-31T12:51:18.418Z - Downloaded in 1 sec(s).
2023-10-31T12:51:18.431Z - Verifying checksum...
2023-10-31T12:51:18.465Z - Extracting contents...
2023-10-31T12:51:19.688Z - performing sha512 checksum for: nodejs...
2023-10-31T12:51:19.876Z - Done in 2 sec(s).
2023-10-31T12:51:19.907Z - Downloading and extracting 'python' version '3.10.8' to '/tmp/oryx/platforms/python/3.10.8'...
2023-10-31T12:51:19.913Z - Detected image debian flavor: bullseye.
2023-10-31T12:51:21.326Z - Downloaded in 2 sec(s).
2023-10-31T12:51:21.343Z - Verifying checksum...
2023-10-31T12:51:21.349Z - Extracting contents...
2023-10-31T12:51:23.829Z - performing sha512 checksum for: python...
2023-10-31T12:51:24.147Z - Done in 5 sec(s).
2023-10-31T12:51:24.180Z - image detector file exists, platform is python..
2023-10-31T12:51:24.193Z - OS detector file exists, OS is bullseye..
2023-10-31T12:51:24.298Z - Python Version: /tmp/oryx/platforms/python/3.10.8/bin/python3.10
2023-10-31T12:51:24.305Z - Creating directory for command manifest file if it does not exist
2023-10-31T12:51:24.311Z - Removing existing manifest file
2023-10-31T12:51:24.335Z - Python Virtual Environment: antenv
2023-10-31T12:51:24.341Z - Creating virtual environment...
2023-10-31T12:51:30.102Z - Activating virtual environment...
2023-10-31T12:51:30.114Z - Running pip install...
(like 4 minutes installing python libraries)

2023-10-31T12:55:37.571Z - Not a vso image, so not writing build commands
2023-10-31T12:55:37.576Z - Preparing output...
2023-10-31T12:55:37.586Z - Copying files to destination directory '/tmp/_preCompressedDestinationDir'...
2023-10-31T12:57:07.095Z - Done in 93 sec(s).
2023-10-31T12:57:07.109Z - Compressing content of directory '/tmp/_preCompressedDestinationDir'...
2023-10-31T13:05:28.569Z - Copied the compressed output to '/home/site/wwwroot'
2023-10-31T13:05:28.603Z - Removing existing manifest file
2023-10-31T13:05:28.641Z - Creating a manifest file...
2023-10-31T13:05:28.675Z - Manifest file created.
2023-10-31T13:05:28.684Z - Copying .ostype to manifest output directory.
2023-10-31T13:05:28.695Z - Done in 853 sec(s).
2023-10-31T13:05:29.132Z - Running post deployment command(s)...
2023-10-31T13:05:29.459Z - Generating summary of Oryx build
2023-10-31T13:05:29.588Z - Parsing the build logs
2023-10-31T13:05:29.703Z - Found 0 issue(s)
2023-10-31T13:05:29.884Z - Build Summary :
2023-10-31T13:05:29.973Z - ===============
2023-10-31T13:05:30.067Z - Errors (0)
2023-10-31T13:05:30.163Z - Warnings (0)
2023-10-31T13:05:30.400Z - Triggering recycle (preview mode disabled).
2023-10-31T13:05:30.534Z - Deployment successful. deployer =  deploymentPath =

字符串
正如您所看到的,大部分时间都花在了“压缩目录'/tmp/_preCompressedDestinationDir'的内容”步骤(853秒)上。
如何加快部署过程?即使不是完全部署,我也需要一种更有效的方法,以便在需要时对代码进行较小的更改。

调查:

我一直在探索App Service的构建系统Oryx,并尝试了Oryx configurations的各种组合来优化部署时间,但我没有看到任何显著的改进。
我很好奇为什么要将虚拟环境压缩到“output.tar.gz”中,然后再解压缩到一个临时文件夹中。有没有办法绕过这个压缩步骤?
另外,有没有办法修改oryx-manifest.toml文件中的变量?添加应用程序变量或添加自定义的oryx-manifest到我的代码中,都没有效果。
任何见解或建议将不胜感激。谢谢!

ecbunoof

ecbunoof1#

  • 方法1:-*

MS Document通过Zip部署部署Azure Web应用程序是提高部署性能的最佳方法。
如果你的Azure Django应用在github仓库中,你可以下载仓库作为Zip文件夹,然后将其部署在Azure Web应用中,如下所示:
我从这个Github repository下载了一个Django示例代码,并通过下面的Zip命令在我的Azure Web应用程序中部署它:
x1c 0d1x的数据
创建Azure Web应用程序后,在Web应用程序配置中添加以下设置:-
因为在启用**WEBSITE_RUN_FROM_PACKAGE的情况下,App Service会直接从挂载的目录启动应用,并将上传的包挂载为只读的wwwroot**目录。请参考此SO thread answer



现在在终端中登录到Azure帐户,设置订阅并运行以下命令来部署Django Web应用:

az webapp deployment source config-zip --resource-group v-sidrg --name siliconwebappdjango --src C:\msdocs-python-django-webapp-quickstart-main.zip

字符串

输出:-

  • 部署成功,不到一分钟:-*



关于Oryx构建参考这个博客了解更多关于Oryx行为的细节。
当您使用Local GitBitbucketExternal GitGitHub Actions时,Oryx默认启用。

  • 方法2:-*
  • 使用venv并在虚拟环境中安装所有软件包,然后通过虚拟环境部署代码。这将有助于直接从venv部署Web应用程序,如下所示:-
py -m venv .venv
.venv\scripts\activate
pip install -r requirements.txt

  • 方法3:-*

使用CI/CD管道通过Azure DevOps,Github Actions,Bitbucket等构建和部署Web应用程序。

Azure DevOps:-

您可以将Python代码发送到Azure DevOps Repos并运行以下管道来构建Python应用并将其部署在Web应用中。一旦在存储库中添加新更改,您的Web应用部署管道将自动触发,从而减少您的时间,如下所示:-
DevOps管道:-

trigger:
- main

variables:
 
  azureServiceConnectionId: 'xxxxx035'

  
  webAppName: 'webapp-name'

  
  vmImageName: 'ubuntu-latest'

  
  environmentName: 'appname'

 
  projectRoot: $(System.DefaultWorkingDirectory)

 
  pythonVersion: '3.10'

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


Github操作:-

您还可以通过Github Actions工作流部署Python应用,方法是将Web应用连接到Github并保存工作流以如下所示运行:

点击保存后,你的Github操作部署将开始,你的Github存储库中的源代码将部署在Web应用程序中。

其中最快的方法是Zip部署,你也可以在你的Github动作工作流中添加zip部署命令.在这个SO线程答案,我已经部署.net应用程序,但通过Github动作使用zip部署命令,你可以使用相同的方法为Python Django Web应用程序.

  • 方法4:-*

将Django应用程序Dockerize并将其部署在Azure Web应用程序中,因为部署容器化应用程序更快,并且有助于隔离应用程序组件。

相关问题