Build CI/CD Pipeline For Azure Web Apps

Introduction

 
In my recent article, I created CI/CD pipeline for the Azure Kubernetes cluster. In this article, I will create CI/CD pipeline for the Azure Web App service because I want to do an automatic build and release process. If any developer makes a change in the code, then these pipelines will run automatically based on the branch name trigger. I already have pushed this application source code to Azure Repo. This is a public repo and anyone can access and clone the code.
 
Prerequisites
 
You are required to have intermediate-level knowledge of Azure Web Apps and Azure DevOps.
 
Road Map
  • Create Azure Resource Group
  • Create Azure App Service Plan 
  • Create Azure WebApp
  • Create a Build Pipeline
  • Create a Release Pipeline
Let's get started.
 
Log in to the Azure portal or log in through Azure CLI. I will show you both ways. Open your command prompt and use the following command in order to login to Azure. Make sure you already have installed the Azure CLI on your local.
  1. az login  
Build CI/CD Pipeline For Azure Container Instances
 
After logging in, you can see your all active subscriptions in the output and you need to set the subscription for this current context. To do so use the following command. I have multiple subscriptions and I will use my Azure pass sponsorship for this context. 
  1. az account set --subscription "Azure Pass - Sponsorship"
 
Step 1 - Create an Azure Resource Group
 
As you know, we already logged in using CLI. Now we will create a resource group for the Azure web app and app service plan. We will keep our all resources in this resource group that we are creating. Use the following command in order to create the resource group.
  1. az group create --name "webapps-rg" --location "centralus" 
 
  1. az group list --output table  
Step 2 - Create Azure App Service Plan
 
We have created a resource group. Now for every new resource, we will add to this resource group. Use the following command to create the Azure app service plan for web apps. I am creating an app service plan for the Linux environment. This step is only required if you are creating all Azure resources through Azure CLI otherwise using the portal will automatically create while creating the web app.
  1. az appservice plan create -g webapps-rg -n asp-webapp --is-linux --number-of-workers 1 --sku S1  
 
 
Step 3 - Create Azure Web App
 
If you already have installed Azure CLI and you are following the CLI commands then use the following command in order to create an Azure web app, otherwise you can follow the Azure portal step as shown below.
  1. az webapp create -g webapps-rg -p asp-webapp --runtime "DOTNETCORE|3.1" -n learning-webapp  
 
If you are creating a web app service through the Azure portal then click on create a resource and choose web app.
 
 
Choose a resource group and put a valid name and choose runtime stack dot net core 3.1 and operating system Linux because this is a very cheap environment. Click on review and create.
 
 
 
 
Step 4 - Create a Build Pipeline
 
Login to your DevOps accounts and create a new project with the name Learning App. Open this newly created project and add source code to this project repo. You can also use the existing source code that I mention on top of this article. I already have the source code in this project.
 
Next, click on the Pipeline tab and then choose the pipelines. This pipeline will build the source code and then create the artifacts and publish the artifacts. Click on create pipeline and choose the source code option. As I mentioned earlier that, I am using Azure Repos for my source code. I will select the Azure repo git option.
 
 
 
  
Select your repository and then branch. In my case, I will choose Learning.
 
 
Next, select a starter pipeline template because I will use my own commands to build any dot net core web app. You can use it for any of your dot net core web apps. 
 
 
 
 
Replace the following code in the azure pipelines file and click on Save and Run. 
  1. # ASP.NET Core  
  2. # Build and test ASP.NET Core projects targeting .NET Core.  
  3. # Add steps that run tests, create a NuGet package, deploy, and more:  
  4. # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core  
  5.   
  6. trigger:  
  7. - master  
  8.   
  9. pool:  
  10.   vmImage: 'ubuntu-latest'  
  11.   
  12. variables:  
  13.   buildConfiguration: 'Release'  
  14.   
  15. steps:  
  16. - script: dotnet build --configuration $(buildConfiguration)  
  17.   displayName: 'dotnet build $(buildConfiguration)'  
  18.     
  19. - task: DotNetCoreCLI@2  
  20.   displayName: 'Create artifacts'  
  21.   inputs:  
  22.     command: 'publish'  
  23.     publishWebProjects: true  
  24.     arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'  
  25.       
  26. - task: PublishPipelineArtifact@1  
  27.   displayName: 'Publish artifacts'  
  28.   inputs:  
  29.     targetPath: '$(Build.ArtifactStagingDirectory)'  
  30.     publishLocation: 'pipeline'  
 
After save and run, You will see your build pipeline is running and building the code and creating the artifacts, and publishing the artifacts.
 
 
 
 
Step 5 -  Create a Release Pipeline
 
We have successfully built the source code and published the artifacts. Next, click on releases and a select new pipeline. From the templates choose the Azure App Service deployment template.
 
 
 
Change the stage name as you like. 
 
 
Click on tasks and select your Azure subscription and App type, App service name, and Startup command. As you can see, I added the dotnet LearningAPI.dll command because I have created the Linux environment therefore this is compulsory to put this startup command.
 
 
 
 
Click on add artifacts and choose the build and select the Source, Default version, and Source alias then hit the Add button.
 
 
 
Enable the continuous deployment trigger by clicking on this event type button and enable the toggle and add the build branch then save the release pipeline. Now our release pipeline is also ready to run. 
 
 
 
Final Words
 
We have successfully created a build and release pipeline for our Azure web app. Add any change to your source code and push it to the repo, then you will see build pipeline will run and build the code and publish the artifacts. Then release pipeline will deploy the latest artifacts to the Azure web app.
 
 
 
 
 
 
Go to the Azure portal and open the web app URL. Yahoo latest web app up and running. So, this was the process of building a CI/CD pipeline for the Azure web app.