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

BEN ABT
medialesson
Published in
4 min readNov 28, 2023

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 1.2.3.0 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 1.2.3.0.
  • 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:

stages:
- stage: Build
pool:
vmImage: 'ubuntu-latest'
variables:
- name: DOTNET_RUNTIMES
value: linux-x64
- name: BUILD_SOLUTION_FILE
value: MyApp.sln
- name: PUBLISH_PROJECT_PATH
value: src/MyApp/MyApp.csproj
- name: ARTIFACTS_DIRECTORY_APP
value: $(Build.ArtifactStagingDirectory)/myapp/
- name: ARTIFACTS_DIRECTORY_APP_ARCHIVE
value: $(Build.ArtifactStagingDirectory)/myapp-archive/
- name: APP_ARCHIVE_NAME
value: myapp-deployment.zip
jobs:
- job: Code
displayName: App Build ${{ variables.DOTNET_RUNTIMES }}
timeoutInMinutes: 20
steps:
- 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
inputs:
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 => https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/48
- powershell: Install-Module Microsoft.PowerShell.Archive -MinimumVersion 1.2.3.0 -Repository PSGallery -Force
displayName: Install PowerShell Modules

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

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

- task: AzureCLI@2
displayName: "Azure Web App Deploy"
inputs:
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 `
--src-path $(ARTIFACTS_DIRECTORY_APP_ARCHIVE)/$(APP_ARCHIVE_NAME)

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

Autor

Benjamin Abt

Ben is a passionate developer and software architect and especially focused on .NET, cloud and IoT. In his professional he works on high-scalable platforms for IoT and Industry 4.0 focused on the next generation of connected industry based on Azure and .NET. He runs the largest german-speaking C# forum myCSharp.de, is the founder of the Azure UserGroup Stuttgart, a co-organizer of the AzureSaturday, runs his blog, participates in open source projects, speaks at various conferences and user groups and also has a bit free time. He is a Microsoft MVP since 2015 for .NET and Azure.

Originally published at https://schwabencode.com on November 28, 2023.

--

--