Deploy ASP.NET Core via Zip Deployment to Azure Web App

Deploy ASP.NET Core via Zip Deployment to Azure Web App

There are various ways to deploy an application, but Zip deployment is the variant that is recommended the most - regardless of whether you are using Azure, AWS or other cloud and hosting providers.

Even .NET recommends this variant out of the box, but unfortunately there has been no built-in CLI tooling for this for many years - and all documentation examples also primarily show file deployments - and not Zip deployments

Advantages of Zip deployment

  • Faster deployment, as only one connection to the target needs to be established
  • One large file can be transferred faster than many small files; this makes the deployment itself faster
  • Since the target unpacks the Zip itself, there is no need for write access from outside, which means that access from outside can be read-only, which increases security => Your Web App files will be read-only!
  • Zip deployment only overwrites files that have changed
  • The Zip deployment deletes files that are no longer present in the new deployment

Azure DevOps

In principle, there are several options for zipping; on Azure DevOps, however, archiving via PowerShell is the best option, as this works on all agents and all operating systems.

Unfortunately, it should be noted that the PowerShell Zip was previously not compatible with the zip standard, which only changed with version of the Microsoft.PowerShell.Archive module. Since then, 7zip is used which you can see in the logs then.

The following steps are necessary for implementation:

  • Install the PowerShell module Microsoft.PowerShell.Archive with at least version
  • Build the application
  • Publish the application with the target runtime (also a recommendation) in a folder
  • Create a folder in which the ZIP file is to be stored
  • Zipping the application files from the publish step
  • Deploy the zip to the WebApp

*In a real environment, there would also be a separation of build and release, so that the zip has to be treated as an artifact, which I am omitting here for demo purposes.

The YAML could therefore be simplified as follows:

  - stage: Build
      vmImage: 'ubuntu-latest'
      - name: DOTNET_RUNTIMES
        value: linux-x64
        value: MyApp.sln
        value: src/MyApp/MyApp.csproj
        value: $(Build.ArtifactStagingDirectory)/myapp/
        value: $(Build.ArtifactStagingDirectory)/myapp-archive/
      - name: APP_ARCHIVE_NAME
    - job: Code
      displayName: App Build ${{ variables.DOTNET_RUNTIMES }}
      timeoutInMinutes: 20
        - checkout: self  # self represents the repo where the initial Pipelines YAML file was found
          clean: "true"  # whether to fetch clean each time
          fetchDepth: "0"  # the depth of commits to ask Git to fetch

        - task: UseDotNet@2
          displayName: Install .NET SDK
            useGlobalJson: true
            workingDirectory: $(Build.SourcesDirectory) # here is your global.json

        - script: dotnet restore $(BUILD_SOLUTION_FILE) --configfile NuGet.config
          displayName: .NET Package Restore

        - script: dotnet build $(BUILD_SOLUTION_FILE) -c Release --no-restore
          displayName: .NET Build

        - script: dotnet publish $(PUBLISH_PROJECT_PATH) --no-restore -c Release -o $(ARTIFACTS_DIRECTORY_APP) --self-contained --runtime $(DOTNET_RUNTIMES)
          displayName: .NET Publish App

        ## PowerShell Setup =>
        - powershell: Install-Module Microsoft.PowerShell.Archive -MinimumVersion -Repository PSGallery -Force
        displayName: Install PowerShell Modules

        ## Create Archive Folder
        - task: PowerShell@2 
          displayName: Create App Zip Folder
            targetType: 'inline'
            script: New-Item -ItemType Directory -Force -Path $(ARTIFACTS_DIRECTORY_APP_ARCHIVE)

        - task: ArchiveFiles@2
          displayName: Create App Zip Archive
            rootFolderOrFile: $(ARTIFACTS_DIRECTORY_APP)
            includeRootFolder: false
            archiveType: 'zip'

        - task: AzureCLI@2
          displayName: "Azure Web App Deploy"
            azureSubscription: # service connection name
            scriptType: pscore
            scriptLocation: inlineScript
            inlineScript: |
              az webapp deploy `
                --resource-group your-resource-group-name `
                --name your-webapp-name `
                --async false --clean true --restart true --type zip `

This would give us a consistent deployment for ASP.NET Core applications to Azur Web Apps.