Azure pipelines: speeding up a Python App Service (Web App) on Linux deployment.

Recently, I created an Azure deployment pipeline for a Python flask application. But the actual deployment step took 10 minutes, which I found unexpectedly long.

How to speed up the deployment step

We are talking about Azure pipelines here (the yaml flavour). There are different deployment methods that you can use with the AzureRmWebAppDeployment task. Let’s have a look at the zipDeploy and runFromPackage deployment methods. Both deployment methods zip your project folder including the virtual environment and use that zip file for the deployment:

  1. zipDeploy: this is the standard deployment method for Azure App Services on Linux. This deployment method seems to scale with the number of files that are included in your zip file. Deleting all pre-compiled .pyc files resulted in a 25% decrease of the deployment time in my case. Before the ArchiveFiles task, run a script step: find . -path "*__pycache__*" -delete
  2. runFromPackage: this deployment method uses a zip archive as well, but mounts it as the wwwroot on the server. Important: your app will not be able to write to the wwwroot folder.
    1. Add the following setting to your App Service’s application settings (Azure portal -> choose your web app -> Configuration -> Application settings -> New application setting - even better, use Terraform or some other infrastructure as code solution): name=WEBSITE_RUN_FROM_PACKAGE, value=1
    2. Make sure all your python files are compiled - our directory will be read-only. Add a script step before the ArchiveFiles task: python -m compileall .
    3. In the AzureRmWebAppDeployment@4 task, add the following two keys to the inputs part:
    - task: AzureRmWebAppDeployment@4
    ...
      inputs:
        ...
        enableCustomDeployment: true
        deploymentMethod: 'runFromPackage'
    

The runFromPackage deployment method decreased the run time of this step from 10 minutes (or 7.5 minutes without .pyc files) to thirty seconds.

How to debug the deployment task

In Azure devops, click on a job run. Check the AzureRMWebAppDeployment task. It prints a link to the detailed logs (“Deploy logs can be viewed at htts://…”).