Manage your Azure deployment slots with Azure CLI and Kudu
By: Ivan Porta
Tags: Coding, Deployment, Cli, Azure, Microsoft

In this article, I’m going to show you how to create and manage Azure deployment slots using Azure CLI.

Creating an Azure Web App Resource

First, you need to login to you Azure account. To do so, execute the following command:

az login

Then you can procced with the creation ofthe resource group, app service plan and only then the web ap resource.

# Login to your Azure account

az login # Create a new resource group

az group create -n training-rg -l westus # Create a new app service plan

az appservice plan create -g training-rg -n training-plan --sku S1 # Create a new Web App resource

az webapp create -g training-rg -p training-plan -n origintechnologiestraining

Add a deployment slot

To create a new slot, use the following command:

az webapp deployment slot create -n origintechnologiestraining -g training-rg --slot staging

Create and deploy a .NET 5 application

Now that your Azure resources is ready, it’s time to create your web application. In this demo, I’m going to use the dotnet CLI to create a new API project.

dotnet new webapi -o Training -f net5.0

To run your application locally run the following command

dotnet run -p Training\Training.csproj

info: Microsoft.Hosting.Lifetime[0]

Now listening on:

info: Microsoft.Hosting.Lifetime[0]

Now listening on:

info: Microsoft.Hosting.Lifetime[0]

Application started. Press Ctrl+C to shut down.

info: Microsoft.Hosting.Lifetime[0]

Hosting environment: Development

info: Microsoft.Hosting.Lifetime[0]

Content root path: C:\Users\ivanp\Desktop\Training Building...info: Microsoft.Hosting.Lifetime[0]Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0]Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0]Application started. Press Ctrl+C to shut down.info: Microsoft.Hosting.Lifetime[0]Hosting environment: Developmentinfo: Microsoft.Hosting.Lifetime[0]Content root path: C:\Users\ivanp\Desktop\Training

Deploy your application

It’s time to deploy your application. To do so, you will have to compile and create a release package of your application and its dependencies, compress the package and then use kudu to push it to Azure. This might sound not very easy by Its just a matter of few commands.

# Compile and publish the application and its dependencies

dotnet publish -f net5.0 -o Release -c Release Training.csproj # Create a ZIP archive of the output folder of the dotnet publish command

Compress-Archive -Path Release/* -DestinationPath release.zip # Deploy the ZIP file to your web app using the kudu zip push deployment

az webapp deployment source config-zip -g training-rg -n origintechnologiestraining --src release.zip

The previous instructions are the same for a deployment slot except the last one, which will also specify the slot name with the -s parameter.

az webapp deployment source config-zip -g training-rg -n origintechnologiestraining --src release.zip -s staging

Congratulation! You have successfully deployed your new API on Azure.

Traffic routing

The routing traffic configure between slots is pretty straightforward with Azure CLI. In fact, you can do it by simply executing the following command:

az webapp traffic-routing set --distribution staging=50 --name origintechnologiestraining -g training-rg

Swap deployment slot

To swap two slots, execute this command in your command prompt:

az webapp deployment slot swap -g training-rg -n origintechnologiestraining --slot staging --target-slot production

Reference