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 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:

 1stages:
 2  - stage: Build
 3    pool:
 4      vmImage: 'ubuntu-latest'
 5    variables:
 6      - name: DOTNET_RUNTIMES
 7        value: linux-x64
 8      - name: BUILD_SOLUTION_FILE
 9        value: MyApp.sln
10      - name: PUBLISH_PROJECT_PATH
11        value: src/MyApp/MyApp.csproj
12      - name: ARTIFACTS_DIRECTORY_APP
13        value: $(Build.ArtifactStagingDirectory)/myapp/
14      - name: ARTIFACTS_DIRECTORY_APP_ARCHIVE
15        value: $(Build.ArtifactStagingDirectory)/myapp-archive/
16      - name: APP_ARCHIVE_NAME
17        value: myapp-deployment.zip
18    jobs:
19    - job: Code
20      displayName: App Build ${{ variables.DOTNET_RUNTIMES }}
21      timeoutInMinutes: 20
22      steps:
23        - checkout: self  # self represents the repo where the initial Pipelines YAML file was found
24          clean: "true"  # whether to fetch clean each time
25          fetchDepth: "0"  # the depth of commits to ask Git to fetch
26
27        - task: UseDotNet@2
28          displayName: Install .NET SDK
29          inputs:
30            useGlobalJson: true
31            workingDirectory: $(Build.SourcesDirectory) # here is your global.json
32
33        - script: dotnet restore $(BUILD_SOLUTION_FILE) --configfile NuGet.config
34          displayName: .NET Package Restore
35
36        - script: dotnet build $(BUILD_SOLUTION_FILE) -c Release --no-restore
37          displayName: .NET Build
38
39        - script: dotnet publish $(PUBLISH_PROJECT_PATH) --no-restore -c Release -o $(ARTIFACTS_DIRECTORY_APP) --self-contained --runtime $(DOTNET_RUNTIMES)
40          displayName: .NET Publish App
41
42        ## PowerShell Setup => https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/48
43        - powershell: Install-Module Microsoft.PowerShell.Archive -MinimumVersion 1.2.3.0 -Repository PSGallery -Force
44        displayName: Install PowerShell Modules
45
46        ## Create Archive Folder
47        - task: PowerShell@2 
48          displayName: Create App Zip Folder
49          inputs:
50            targetType: 'inline'
51            script: New-Item -ItemType Directory -Force -Path $(ARTIFACTS_DIRECTORY_APP_ARCHIVE)
52
53        - task: ArchiveFiles@2
54          displayName: Create App Zip Archive
55          inputs:
56            rootFolderOrFile: $(ARTIFACTS_DIRECTORY_APP)
57            includeRootFolder: false
58            archiveType: 'zip'
59            archiveFile: $(ARTIFACTS_DIRECTORY_APP_ARCHIVE)/$(APP_ARCHIVE_NAME)
60
61        - task: AzureCLI@2
62          displayName: "Azure Web App Deploy"
63          inputs:
64            azureSubscription: # service connection name
65            scriptType: pscore
66            scriptLocation: inlineScript
67            inlineScript: |
68              az webapp deploy `
69                --resource-group your-resource-group-name `
70                --name your-webapp-name `
71                --async false --clean true --restart true --type zip `
72                --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.


Comments

Twitter Facebook LinkedIn WhatsApp