Partager via


Run From Package and Slot Swap With Preview

Run From Package deployment has many advantages and makes DevOPs life much easier. No more live editing, no more file in use error, better cold start performance, same version gets deployed across multiple AppServices, etc. For more details please click on this link

Slot Swap with Preview options allows you to do a final sanity check and warmup the website before taking on production load. For more details please see this link and this one

FYI, for high availability and better network performance, you should consider hosting your website in multiple regions and configuring a Azure Traffic Manager. For more details please see this link. Websites with heavy graphic, it is recommended to use CDN and upload all media files to Azure Storage. For more details please click on this link. This will reduce the size of package zip file.

Combining these above two features, we will be able to do better controlled deployment with least amount of downtime.

Here is sample PowerShell script to streamline deployments using these two features : 

Main steps in this PowerShell scripts are

  1. Upload the zip file to Azure Storage and get SAS url
  2. Add this SAS url to the appsetting of AppService website
  3. Initiate Slot Swap With Preview
  4. Test the slot website and do the warmup
  5. Commit Slot Swap With Preview
 
#Login-AzureRmAccount

# provide your web package name
$zipFilePath = ".\V1.zip"

# provide your storage account detail 
$storageAccountName = "runaspacakge"
$storageResourceGroupName = "runaspackagecentralus"
$containerName = "mypackage"

# provide your website details
$websiteResourceGroupName = "runaspackagecentralus"
$slotName = "preprod"
$website1Names = "runaspackageeastus", "runaspackagecentralus"



# set the package name to current datetime
$uploadPackageName = (Get-Date).ToString("yyyyMMddHHmmss")

Write-Host "New package name will be "$uploadPackageName".zip" 

Write-Host "trying to get storage..."
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName `
                                -Name $storageAccountName

Write-Host "trying to get storage context..."
$ctx = $storageAccount.Context

Write-Host "trying to upload to blob..."
$blobupload = Set-AzureStorageBlobContent -File  $zipFilePath `
                                -Container $containerName `
                                -Blob $uploadPackageName `
                                -Context $ctx 

Write-Host "trying to get SAS URL..."
$sastokenURL = New-AzureStorageBlobSASToken -Container $containerName -Blob $uploadPackageName -Context $ctx -Permission rwdl -FullUri

foreach($website1Name  in $website1Names )
{
    Write-Host $website1Name
    Write-Host "`t trying to get website..." 
    $webApp =  Get-AzureRmWebAppSlot -ResourceGroupName $websiteResourceGroupName `
                        -Name $website1Name `
                        -Slot $slotName


    Write-Host "`t trying to get appsettings..."
    $currentAppSettings = $webApp.SiteConfig.AppSettings
 
    $appsettings = @{}

    ForEach ($kvp in $currentAppSettings) {
    $appsettings[$kvp.Name] = $kvp.Value
    }

    # update package URL
    $appsettings['WEBSITE_RUN_FROM_PACKAGE'] = "$sastokenURL"

    Write-Host "`t trying to set appsetting..."
    $setappSetting = Set-AzureRMWebAppSlot -ResourceGroupName $websiteResourceGroupName `
                                -Name $website1Name `
                                -AppSettings $appsettings `
                                -Slot $slotName `

}

$ans = Read-Host 'Start slot swap with preview? (Y/N)'
if( $ans -eq 'Y' -OR $ans -eq 'y')
{
 
}
else
{
    Exit
} 

foreach($website1Name  in $website1Names )
{
    Write-Host $website1Name
    Write-Host "`t trying to swap with preview..."
    Switch-AzureRmWebAppSlot -ResourceGroupName $websiteResourceGroupName `
        -Name $website1Name `
        -SourceSlotName $slotName `
        -DestinationSlotName production `
        -SwapWithPreviewAction ApplySlotConfig

    Write-Host "`t sending a test web request to slot website..."
    $response = Invoke-WebRequest -Uri $webApp.HostNames[0]
    write-host "`t test request status code is " $response.StatusCode

    Write-Host "`t test slot website now...https://"$webApp.HostNames[0]
    $ans = Read-Host "`t Commit this swap? (Y/N)"

    if( $ans -eq 'Y' -OR $ans -eq 'y')
    {
        Write-Host "`t`t Completing slot with preview swap..."
        Switch-AzureRmWebAppSlot -ResourceGroupName $websiteResourceGroupName `
                    -Name $website1Name `
                    -SourceSlotName $slotName `
                    -DestinationSlotName production `
                    -SwapWithPreviewAction CompleteSlotSwap
    }
    else
    {
        Write-Host "`t`t Canceling slot with preview swap..."
        Switch-AzureRmWebAppSlot -ResourceGroupName $websiteResourceGroupName `
                -Name $website1Name `
                -SourceSlotName $slotName `
                -DestinationSlotName production `
                -SwapWithPreviewAction ResetSlotSwap
    }
}
Write-Host "DONE"

Here is screenshot of output of this above PowerShell script :

image

If there are any errors downloading the zip file from blob storage, you should see FAILED TO DOWNLOAD ZIP FILE.txt in the wwwroot folder as shown below:

image