Deploying ASP.NET Core 6 or Higher Applications to Azure Web Apps with Azure DevOps

Deploying ASP.NET Core applications to Azure Web Apps can be streamlined with Azure DevOps, ensuring that your application is always up-to-date with the latest code changes. This article will guide you through setting up a Continuous Integration/Continuous Deployment (CI/CD) pipeline, breaking down each part of the process for better understanding.

Prerequisites

Before we start, ensure you have:

  • An Azure account with an active subscription.
  • An Azure DevOps account with a project set up.
  • An ASP.NET Core 6 web application ready for deployment.
  • An Azure Web App created for your application.
Continuous Integration Pipeline

The CI pipeline automates the build and testing of your application. It’s triggered by changes to your codebase, specifically the main branch in this example.

Trigger and Agent Pool
trigger:
- main

pool:
vmImage: 'windows-latest'
  • Trigger: Specifies that the pipeline runs on commits to the main branch.
  • Pool: Defines the virtual machine image to use for the pipeline. windows-latest is chosen here, suitable for .NET applications.
Restore Dependencies
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'
  • This task uses the .NET Core CLI to restore the NuGet packages required for your ASP.NET Core project.
Build Project
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration Release'
  • Compiles the application in the Release configuration, ensuring your code is ready for production.
Run Tests
- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: '**/*Tests/*.csproj'
arguments: '--configuration Release'
  • Executes unit tests in your solution, helping catch issues early in the development cycle.
Publish Application
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: true
  • Publishes the web application to a directory and zips the contents, preparing it as an artifact for deployment.
Publish Artifact
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
  • This task publishes the zipped application as an artifact named drop, making it available for the deployment process.
Continuous Deployment Pipeline

The CD pipeline takes the artifact generated by the CI pipeline and deploys it to Azure Web Apps.

Download Artifact
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(System.ArtifactsDirectory)'
  • Retrieves the artifact created by the CI pipeline.
Deploy to Azure Web Apps
- task: AzureRmWebAppDeployment@4
inputs:
ConnectionType: 'AzureRM'
azureSubscription: '<Your-Azure-Service-Connection-Name>'
appType: 'webApp'
WebAppName: '<Your-Azure-App-Service-Name>'
packageForLinux: '$(System.ArtifactsDirectory)/drop/**/*.zip'
  • Deploys the artifact to an Azure Web App. You’ll need to replace placeholder values with your actual Azure service connection name and Web App name.
Complete YAML Configuration for Azure DevOps

Below is the entire YAML file that encapsulates both the Continuous Integration (CI) and Continuous Deployment (CD) pipelines. This configuration allows for an automated process, from code updates to deploying the latest version to Azure Web Apps.

trigger:
- main

pool:
vmImage: 'windows-latest'

steps:
# Restore dependencies
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'

# Build the project
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration Release'

# Run unit tests
- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: '**/*Tests/*.csproj'
arguments: '--configuration Release'

# Publish the web application
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: true

# Publish the build artifact
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'

# Download the build artifact
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(System.ArtifactsDirectory)'

# Deploy to Azure Web Apps
- task: AzureRmWebAppDeployment@4
inputs:
ConnectionType: 'AzureRM'
azureSubscription: '<Your-Azure-Service-Connection-Name>'
appType: 'webApp'
WebAppName: '<Your-Azure-App-Service-Name>'
packageForLinux: '$(System.ArtifactsDirectory)/drop/**/*.zip'
Instructions for Use:
  1. Replace Placeholder Values: Ensure you replace <Your-Azure-Service-Connection-Name> with the name of your Azure service connection and <Your-Azure-App-Service-Name> with the name of your Azure Web App. These placeholders are in the deployment task towards the end of the YAML file.
  2. Commit the YAML to Your Repository: Save this YAML file in your repository, typically at the root or in a .azure-pipelines directory. Name it in a way that reflects its purpose, for example, azure-pipelines.yml.
  3. Set Up Your Pipeline in Azure DevOps: In Azure DevOps, create a new pipeline and select this YAML file as the source. Azure DevOps will parse the YAML and set up the pipeline according to the steps defined.
  4. Test Your Pipeline: Make a change in your main branch to trigger the pipeline, or manually trigger a build in Azure DevOps. Monitor the build and deployment process to ensure everything works as expected.

This YAML file provides a robust foundation for automating the deployment of ASP.NET Core 6 applications to Azure Web Apps. By leveraging Azure DevOps pipelines, you’re not only streamlining the deployment process but also ensuring that your application is always running the latest version of the code, facilitating continuous improvement and delivery.

Conclusion

Implementing a CI/CD pipeline with Azure DevOps for deploying ASP.NET Core applications to Azure Web Apps is a significant step towards achieving modern, agile software development and deployment practices. This approach not only automates the build and deployment processes, reducing the potential for human error, but also ensures that your application can be continuously updated with new features, improvements, and bug fixes. As a result, your development team can focus more on delivering value and less on the operational aspects of deployment.

Moreover, the use of Azure DevOps for these purposes integrates seamlessly with Azure’s ecosystem, providing a robust, scalable platform for hosting your applications. The flexibility and control offered through the YAML pipeline configuration allow for custom workflows that match your project’s specific needs, ensuring that your deployment process is as efficient and effective as possible.

By adopting this CI/CD pipeline, you’re not only leveraging best practices for software development but also positioning your team to respond more quickly to changes, meet customer needs more effectively, and maintain a competitive edge in the fast-paced world of software development. Whether you’re working on a small project or a large-scale enterprise application, the principles and practices outlined in this guide will serve as a valuable foundation for your continuous integration and deployment efforts.

Reference

Image source: https://pixabay.com/illustrations/cloud-computing-network-internet-2001090/

Published by Allan Mangune

I hold the esteemed qualification of a Certified Public Accountant and have earned a Master's degree in Science with a specialization in Computer Information Systems. Since entering the realm of software development in 2000, my focus has been on adopting secure coding practices, an endeavour I have intensified after receiving my Certified Ethical Hacker v5 certification in 2008. My professional journey includes guiding clients through their digital transformation journey, particularly emphasizing digital security issues. For more than ten years, I have provided Agile Project Management training to well-known companies. I am a Certified ScrumMaster and have completed the Prince2 Agile Foundation certification. I had the privilege of being recognized as a Microsoft MVP for ASP.NET for ten consecutive years. Previously, I also served as a Microsoft Certified Trainer. As a hobby, I enjoy assembling personal unmanned aerial vehicles during my downtime.

Leave a comment