Sunday, 25 August 2019

Creating an Azure Web App Supporting .NET Core 3 – IaC with Azure Pipelines


We have discussed how to build a .NET Core 3 Web Application in the previous post. In order to deploy a .NET Core 3.0 Web App to an Azure Web App, you need to install .NET Core 3.0 Extension to the Azure Web App. You can easily add .NET Core 3.0 via Azure Portal to a Web APP. However, if you are really into automating your infrastructure as code (IaC) you may want to make all these steps automated and executed via a deployment pipeline. Let’s look at a script which is using Azure CLI and Azure PowerShell to fully automate creation of a .NET Core 3.0 enabled Azure Web App.
You can download the full script from here. Let’s understand each part of the script.


The Parameters


Since this script is using Azure CLI and AzureRM PowerShell you need to supply an Azure service principal details to this script. Let’s discuss how to create this service principal later in this post. Parameters starting with $azure refers to service principal details. The script begins by creating a resource group in Azure in a given datacenter. Because of that reason the service principal you have to create must allow subscription level contribute role. For the $core3Extention parameter you can supply either AspNetCoreRuntime.3.0.x64 or AspNetCoreRuntime.3.0.x86 depending on your application need. If you are using free pricing tier in Azure Web App you can only use AspNetCoreRuntime.3.0.x86 with Core 3.0 and Blazer.
A try catch block is used to catch any exception and output the error massages and fail the pipeline in case of an error.
Login


The first step is login to Azure using the service principal.

Resource Group


Script checks if the resource group exist in the subscription and creates a one if it does not exists.

App Service Plan


Next script checks if the web app service plan exists. If exists pricing tier can be updated. If not exists it will create an app service plan in the resource group.

Web App


As the next step a web app with the given name is created if it is not already created and pricing plan is assigned to the web app.

.NET Core 3.0 Extension


The script then checks the .NET Core 3.0 extension is added to the site and add it if not already added.

Slots


If you supply the slot names in an array the script will create the slots for the Azure Web App and add the .NET Core 3.0 extension to each slot. Slots are optional and you can skip this parameter in the script. If you are using slots the pricing plan SKU should support slots.

Exceptions


The exception is thrown in any error in script execution.

Using in Pipeline
To use this script in Azure Pipelines you need to create a service principal in Azure. You can execute Azure CLI command below to create a service principal with contributor role for the Azure subscription.
az ad sp create-for-rbac -n InfraDeploySpn --role contributor --scopes /subscriptions/subscriptionid
Execute the command in Azure cloud shell and copy the service principle details to Azure Pipeline variables.


Then you can use a PowerShell task like below to execute the script to create the Azure Web App with .NET Core 3.0 support.

Once executed you can see the Web App gets added with Core 3.0 extension.

































No comments:

Popular Posts