How to deploy Azure app service using Azure pipeline

deploy-azure-app-service-azure-pipeline

This article talks about deploying an Azure app service using the Azure pipeline. Besides, it covers a YAML based multi-stage Azure pipeline for Azure web app deployment. The multi-stage platform consists of different stages which are further broken down into jobs, steps, tasks etc. I’ll use the Azure pipeline with two stages to deploy an application to the Azure app service

  • Build the application
  • Deploy the application including infrastructure provisioning

To keep things simple, I’ll skip the code quality, automated test etc. The following diagram gives you an idea of how an end-to-end pipeline looks like

ci-cd-azure-pipeline

There are two ways to deploy app service in Azure i.e., manual deployment and automated deployment. Manual deployment is easy to do. You can build and deploy the application using the Azure portal and any other IDEs available out there, also called right-click publishing. On the other hand, the automated deployment is trigger-based and requires the Azure pipeline to do the same. It is always recommended to use an automated deployment to reduce the person dependency. Therefore you’ll use the following services and tools to perform an automated deployment of an application to the Azure app service

  • Azure pipeline (part of Azure DevOps Services) to build and deploy the application to Azure app service
  • Azure Resource Manager (ARM) templates to provision the infrastructure like Azure app service, Azure SQL etc. and run them from Azure pipeline.

Azure pipelines are available in two forms i.e; Classic pipeline and YAML based pipeline. The advantage of using the YAML based Azure pipeline is reusability and maintainability. It is a text-based file stored in the GitHub repository next to the application code. Likewise, ARM templates also store in the GitHub repository, idempotent in nature, and spin up the infrastructure every time it runs. The practice of maintaining the deployment artefacts like pipeline, ARM scripts etc. in a source control system like GitHub is called Infrastructure as Code (IaC). You’ll refer to the following step-by-step guides for deploying an Azure app service using YAML based Azure pipeline and ARM templates

  • Developing a .NET 5.0 Web API with SQL database as a data source
  • Set up the free Azure DevOps account and link it to the GitHub repository
  • Use the Azure pipeline script to deploy the application

Develop a .NET 5 Web API

You’ll need an ASP.NET Core application for app service deployment in Azure. A getting started tutorial to build, run and test a web api is available ASP.NET Core web api with Swagger | .NET 5 – Getting started. You can follow it to

  • Develop an ASP.NET Core web api
  • Integrate with SQL database to perform a CRUD operation
  • Integrate with Swashbuckle to document the web api

To follow the progression from the beginning, please be sure to first complete the set-up of the web api in your local machine.

Set up the free Azure DevOps account and link it to GitHub repository

In this step, you’ll link the Azure DevOps with the GitHub repository. I assume you already have the accounts set up. The link is required because the Azure pipeline will build, provision, and deploy the artefacts stored in the GitHub repository. You can create a new Azure pipeline and selects GitHub as a code repository. During that time, the link will set up between Azure DevOps and the GitHub repository. Accordingly, the Azure pipeline can point to the following YAML script available in GitHub.

Deploying an Azure app service using Azure pipeline

The Azure pipeline can run the YAML file either manually or as part of the CI/PR commit request automatically. The above pipeline runs every time because all triggers (specified as *) are mentioned. Also, it consists of the following steps under Build and Deploy stage

  • Use .NET Core SDK – It installs the .NET 5 SDK to the build machine
  • Use NuGet – It installs the NuGet to the build machine
  • Restore app dependencies – It runs the dotnet restore command to restore the dependencies
  • Build the app – It runs the dotnet build command to build the application
  • Publish the app – It runs the dotnet publish command to package the application in zip format
  • Publish the build artefacts – It publishes the build artefacts to a folder named drop
  • Provision Azure resources – It runs the ARM templates with other linked ARM templates to spin up a resource group, SQL server, SQL database, app service plan, and app service
  • Download the build artefacts – It downloads the build artefacts generated in the build stage
  • Deploy the packaged app to Azure web app – It deploys the zip package to the Azure app service

The Azure pipeline triggers automatically after pushing the changes to the branch. Though you can trigger it manually in Azure DevOps. It executes the steps to build and deploy the application to the Azure app service. Post the Azure web app deployment, you can access the application by its URL (the Azure app service https URL)

This tutorial gives you an overview to deploy a web api to Azure app service using Azure pipeline and ARM template. The code used in this article is available in the Github repository eKart. I hope you find this article useful. Please share your feedback below in the comments section and I’ll respond to them as early as possible.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.