Tuesday, November 22, 2022

Enabling Clean Deployment with zipDeploy in Azure App Service Deploy Task

Azure App Service deploy task capable of doing several deployments such as web deploy, and zip deploy. Web deploy method supports clean deployments to the Azure app service app, but zip deploy doesn't have option to do a clean deployment. But we may want to use zipDeploy specially with Linux hosted agents as described in the post here

To enable clean deployment with zipdeploy, we need to add cleanup step to deployment pipeline before doing the deployment with Azure app service deploy task.  This blog has a PowerShell script which can be used to cleanup wwwroot folder of the app service before new deployment. We can modify the script in that blog, to work with multiple slots in Azure app services and use as a task in Linux agents.

You need to pass three parameters to the following script.
ResourceGroupName : Resource group name of the Azure app service, 
WebAppName: Azure app service name
 slotName: If you are deploying to the production slot, use "Production" as the slot name. if you are deploying to another slots, provide the correct slot name.

$ResourceGroupName="rg-winbuild-linuxdeploy"
$WebAppName = "app-linuxdeploy"
$slotName = 'Production'
                                          
 # Get publishing profile for web application
 if ($slotName -eq 'Production')
       {
        $publishingProfile = az webapp deployment list-publishing-profiles --name $WebAppName --resource-group $ResourceGroupName | ConvertFrom-Json
       }
 else
       {
        $publishingProfile = az webapp deployment list-publishing-profiles --name $WebAppName --resource-group $ResourceGroupName --slot $slotName | ConvertFrom-Json
       }
                                          
# Create Base64 authorization header
$username = $publishingProfile[0].userName
$password = $publishingProfile[0].userPWD
$kuduAppName = $publishingProfile[0].publishUrl -replace ':443','';
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password))) $bodyToPOST = @{ command = "find . -mindepth 1 -delete" dir = "/home/site/wwwroot" }
# create rest call  
      $restcall = @{  
               # command REST API url  
               Uri = "https://$kuduAppName/api/command"  
               Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
               Method = "POST"  
               Body = (ConvertTo-Json $bodyToPOST)  
               ContentType = "application/json"  
              }  
# Invoke REST call  
Invoke-RestMethod @restcall



No comments:

Post a Comment