Azure Appservice for Django fails with Azure Devops Pipeline
I have an appservice running on Azure in a Linux environment.
I try to associate it with a Azure Devops Pipeline so I can have CI/CD for the repositories & branches on Github.
I want the builds to occur on Azure Pipeline & Azure infrastructure, not Github Actions.
I have tried all other suggestions including following environment variables on App Service :
SCM_DO_BUILD_DURING_DEPLOYMENT=true
WEBSITE_NODE_DEFAULT_VERSION=~18
WEBSITE_RUN_FROM_PACKAGE=true
Here is the logs that I receive from the Kudu / Azure Devops Pipeline / AppService logs :
/home/LogFiles/2024_08_14_ln1xsdlwk0000K3_docker.log (https://project-app-development.scm.azurewebsites.net/api/vfs/LogFiles/2024_08_14_ln1xsdlwk0000K3_docker.log)
2024-08-14T23:51:45.168Z INFO - Status: Image is up to date for 10.1.0.6:13209/appsvc/python:3.10_20240619.2.tuxprod
2024-08-14T23:51:45.179Z INFO - Pull Image successful, Time taken: 0 Seconds
2024-08-14T23:51:45.223Z INFO - Starting container for site
2024-08-14T23:51:45.223Z INFO - docker run -d --expose=8000 --name project-app-development_0_f2e0544d -e WEBSITE_USE_DIAGNOSTIC_SERVER=false -e WEBSITE_SITE_NAME=project-app-development -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=project-app-development.azurewebsites.net -e WEBSITE_INSTANCE_ID=3f627dbbecdaed255d87aa9c3e8f1448758df1cdff41f5e14b114384ea9b244a appsvc/python:3.10_20240619.2.tuxprod gunicorn -b :$PORT project.wsgi
2024-08-14T23:51:45.223Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-08-14T23:51:45.893Z INFO - Initiating warmup request to container project-app-development_0_f2e0544d for site project-app-development
2024-08-14T23:51:49.043Z ERROR - Container project-app-development_0_f2e0544d for site project-app-development has exited, failing site start
2024-08-14T23:51:49.057Z ERROR - Container project-app-development_0_f2e0544d didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2024-08-14T23:51:49.063Z INFO - Stopping site project-app-development because it failed during startup.
/home/LogFiles/2024_08_14_ln1xsdlwk0000WT_default_docker.log (https://project-app-development.scm.azurewebsites.net/api/vfs/LogFiles/2024_08_14_ln1xsdlwk0000WT_default_docker.log)
2024-08-14T00:05:39.611783454Z File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-08-14T00:05:39.611788754Z File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2024-08-14T00:05:39.611793955Z File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2024-08-14T00:05:39.611799355Z File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
2024-08-14T00:05:39.611804655Z ModuleNotFoundError: No module named 'project'
2024-08-14T00:05:39.611810055Z [2024-08-14 00:05:39 +0000] [83] [INFO] Worker exiting (pid: 83)
2024-08-14T00:05:39.756375163Z [2024-08-14 00:05:39 +0000] [49] [ERROR] Worker (pid:83) exited with code 3
2024-08-14T00:05:39.758705055Z [2024-08-14 00:05:39 +0000] [49] [ERROR] Shutting down: Master
2024-08-14T00:05:39.758726256Z [2024-08-14 00:05:39 +0000] [49] [ERROR] Reason: Worker failed to boot.
Here is my startup command on Azure AppService Configuration :
gunicorn -b :8000 project.wsgi
I have checked your Azure DevOps YAML Pipeline sample. It has some issues which will lead to the App service not able to work.
From the original log, it shows the error: ModuleNotFoundError: No module named 'project'
. The cause of the issue is that the zip package doesn't contain the required python packages.
You can make the following changes to the Azure Pipeline YAML Sample:
1.You need to change the command to install python packages:
From
pip install -r requirements.txt
To
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
In this case, the required packages will be installed to the correct folder and will be added to the zip pacakge.
2.You need to set the includeRootFolder field to false in ArchiveFiles@2 task.
If the value is set to true, it will include the parent folder : s
to the zip package. This will cause the path issue in Web App. So we need to set the value to false.
Here is an example:
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
Full Pipeline YAML sample:
trigger:
- $(feature-branch)
variables:
azureServiceConnectionId: '-db5f-4993-91f8-85f3a75cb357'
webAppName: 'project-app-development'
vmImageName: 'ubuntu-latest'
environmentName: 'project-app-development'
projectRoot: $(System.DefaultWorkingDirectory)
pythonVersion: '3.8'
system.debug: true
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 virtualenv env
source env/bin/activate
python -m pip install --upgrade pip
pip install --target="./.python_packages/lib/site-packages" -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 : project-app-development'
inputs:
azureSubscription: $(azureServiceConnectionId)
appName: $(webAppName)
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
For more detailed info, you can refer to this doc: Use Azure Pipelines to build and deploy a Python web app to Azure App Service