Using Deployment Slots with Azure Web Apps

Last updated

Deployment Slots provide a nice way to implement Blue-Green deployments for Azure Web Apps.

This provides many benefits, including:

  • Reduced down-time when deploying.
  • When deploying packages with a large number of files, deployment times can be significantly reduced due to not having to compare with existing files (this assumes you are deploying to a clean slot).
  • Roll-back can be achieved by simply swapping the slots back.

Deployment Slots are only available to Azure Web Apps running in Standard or Premium App Service plans

Walk-Through

Here we will give an example of how to setup a Blue-Green deployment for an Azure Web App using Deployment Slots.

Step 1: Create an Azure Web App Deployment Target

Follow the steps for Azure Web App targets.

Step 2: Create Staging Slot

Create a Run an Azure PowerShell Script step.

Assuming you have a variable named 'WebSite' that contains the name of your Azure Web Site, your script should be:

Azure Service Management

#Remove the staging slot if it exists
Remove-AzureWebsite -Name #{WebSite} -Slot Staging -Force

#Create the staging slot
New-AzureWebsite -Name #{WebSite} -Slot Staging

Azure Resource Manager

#Remove the staging slot if it exists
Remove-AzureRMWebAppSlot -Name #{WebSite} -Slot Staging -ResourceGroupName MyResourceGroup -Force -ErrorAction Continue

#Create the staging slot
New-AzureRMWebAppSlot -Name #{WebSite} -Slot Staging -ResourceGroupName MyResourceGroup

The reason for the first line, which removes the Staging Slot, is to ensure we are deploying to a clean slot.  This can significantly reduce the time taken for deployments with a large number of files.

So your step should look like:

Step 3: Deploy Your Package

The next step is to deploy your package to the Staging slot.  We do this by creating a Deploy an Azure Web App step.

You can enter the name of deployment slot in the Deployment Slot field, or to use a variable for the Slot name, click the "Bind" Bind button, and enter

#{WebAppSlotName}

As shown below:

You can choose to specify the slot directly on the deployment target, or directly on the step (if you wish to deploy to multiple different slots on the same Web App Service, for example), however, the slot on the target will take priority.

Step 4: Swap the Staging and Production Slots

The final step is to create another Azure PowerShell step to swap the Staging and Production slots.

Use the PowerShell:

Azure Service Management

#Swap the staging slot into production
Switch-AzureWebsiteSlot -Name #{WebSite} -Slot1 Staging -Slot2 Production -Force

Azure Resource Management

#Swap the staging slot into production
Switch-AzureRmWebAppSlot -ResourceGroupName #{ResourceGroup} -Name #{Website} -SourceSlotName Staging -DestinationSlotName Production

So your step will appear as:

At this point you should have a working Blue-Green deployment process for your Azure Web App.