Azure  

How to swap deployment slots in Azure App Service?

Article Overview

  1. Introduction

    1. What is deployment slots?

    2. Benefits of deployment slots

  2. Download, create and deploy web app

    1. Download web app

    2. Create and Deploy web app

  3. Deploy updated code to the deployment slot

    1. Create staging deployment slot

    2. Update code

    3. Deploy to staging slot

  4. Swap from staging to production slots

  5. Summary

1. Introduction

In this example, first deploy a website to Azure App Service, then create a staging deployment slot, then make changes in code and deploy it to staging slot, and lastly swap from slots from staging to production to reflect the changes in production. This is being performed using Azure Cloud Shell and the Azure portal.

1.1. What are deployment slots?

A deployment slot is one of the powerful features in Azure App Service used to host multiple versions of one web application within the same App Service plan.

The deployment slot is the live environment slot of the web app. Production is the main slot, and one can create additional slots such as Staging or Testing. All slots share the same App Service plan. Different slots run a different application version as specified.

Slot's URL format:

  
    http://<AppName>-<SlotName>.azurewebsites.net
  

1.2. Benefits of deployment slots

The following are the key benefits of using deployment slots:

  • No downtime: One can deploy to a staging slot and perform the testing on it, and then simply swap it with production with no downtime.

  • Separate configuration: each slot can have its own separate configuration settings, such as connection strings or application settings.

  • Production testing : each slot shares have the same infrastructure. Hence, one can perform performance testing and behavior in a production-like environment.

  • Release control: it allows good release control and transition.

  • Easy rollbacks: In case of something goes wrong, one can easily and quickly swap back to the previous version.

Swap slots from staging to production

Image: Swap slots from staging to production

2. Download, create, and deploy a web app

Now, first download the web app from the git, second create a web app on Azure, and last deploy the web app. Here, Azure Cloud Shell is used for this operation, which is an integrated command-line interface.

2.1. Download web app

Let's create a directory, clone the code, and set variables for the resource group and app names. Run the following commands in the sandbox Bash

mkdir statichtmlwebapp
cd statichtmlwebapp

Output: It will create a statichtmlwebapp directory and move into it

git clone https://github.com/Azure-Samples/html-docs-hello-world.git

Output: It will clone the app repository to the statichtmlwebapp directory

resourceGroup=rg-mywebapp
appName=mywebapp$RANDOM
echo $appName

Output: It will set variables to hold the resource group name and the app name

2.2. Create and Deploy Web App

Let’s deploy the web app. Navigate to the directory having the sample code and run the az webapp up command.

cd static-html-web-app
az webapp up -g $resourceGroup -n $appName --html

Output: Change directory to the sample code. Deploy using the az webapp up command

It might take a few minutes to run. During command execution, it displays information like the following:

{
"app_url": "https://<AppName>.azurewebsites.net",
"location": "westeurope",
"name": "<app_name>",
"os": "Windows",
"resourcegroup": "<resource_group_name>",
"serverfarm": "appsvc_asp_Windows_westeurope",
"sku": "FREE",
"src_path": "/home/<username>/demoHTML/static-html-web-app",

After the deployment, one can access it from the browser using the URL below:

https://<AppName>.azurewebsites.net

3. Deploy updated code to the deployment slot

Let’s create a staging deployment slot and update or modify the code, and then deploy that updated code to a new staging slot.

3.1. Create staging deployment slot

In the cloud shell, run the az webapp deployment slot create command to create a deployment slot named staging.

az webapp deployment slot create -n $appName -g $resourceGroup --slot staging

A slot can be created using the Azure portal as well through the following steps:

Step 1: In the Azure portal, select Add Slot in the toolbar to open the Add a slot panel.

Add Slot

Image: Azure portal Add Slot

Step 2: In the Add a slot panel, enter the essential details and click on the Add button to save the changes.

3.2. Update code

Let’s update the content of the web app. In Cloud Shell, type code command to open a file in the editor.

code index.html

Output: index.html file will be opened in the editor. Here, change the content as you like and save it by using the command ctrl-s and exit by ctrl-q.

3.3 . Deploy to staging slot

For this next step, either a zip or a web application resource (WAR) file is required. In the cloud shell, run the zip command to create a zip file of the updated web app.

zip -r stagingcode.zip .

Output: It will create a zip folder with the name "stagingcode"

az webapp deploy -g $resourceGroup -n $appName --src-path ./stagingcode.zip --slot staging

Output: deploy updated zip into the staging slot

One can get the latest updated application changes from the staging URL below.

http://<AppName>-staging.azurewebsites.net

To verify the swap in the Azure portal, select Swap in the toolbar to open the Swap panel.

Slots

Image: Azure portal Slots

4. Swap from staging to production slots

Now, once validation completes, we can swap staging to production using the command az webapp deployment slot swap

az webapp deployment slot swap  -g $resourceGroup -n $appName --slot staging \ --target-slot production

Swap can be done from the Azure portal as well through the following steps:

Step 1: In the Azure portal, select Swap in the toolbar to open the Swap panel.

Swap

Image: Azure portal select Swap

Step 2: In the swap panel, review the settings such that Source should show the -staging slot and Target should show the default production slot.

Azure portal Swap panel

Image: Azure portal Swap panel

Click on the Swap button at the bottom and wait for the operation to complete. One can verify the swap from the application URL or from the current production slot details.

Application URL will reflect the latest version of the application

http://<AppName>-staging.azurewebsites.net

5. Summary

Here, a deployment slot has been created and swapped using Azure Cloud Shell and Azure portal with git clone, az webapp up, az webapp deployment slot create, az webapp deployment slot swap, etc. commands..