CI/CD Pipeline for a .NET Web API Application in Azure DevOps

In today's fast-paced software development landscape, the effective delivery of applications requires seamless automation and integration throughout the development lifecycle. Continuous Integration/Continuous Deployment (CI/CD) pipelines play a pivotal role in achieving this, enabling developers to build, test, and deploy their code efficiently. For .NET Web API applications, Azure DevOps offers a robust platform to establish a streamlined CI/CD pipeline.

Setting Up the Environment

Before diving into creating the CI/CD pipeline, ensure you have the following prerequisites:

  1. Azure DevOps Account: Create an account on Azure DevOps if you haven’t already.
  2. .NET Web API Application: Have a .NET Web API project stored in a repository, such as Azure Repos or GitHub.

Steps to Create a CI/CD Pipeline

Step 1. Create a New Pipeline in Azure DevOps

  1. Navigate to Azure DevOps: Go to your project and select Pipelines > Create Pipeline.
  2. Connect to Your Repository: Choose your repository source (Azure Repos, GitHub, Bitbucket, etc.) and select your project repository.
  3. Select a Template: Choose Starter pipeline or Use the classic editor based on your preference.

Step 2. Define Build Configuration (CI)

Here's a sample YAML configuration for a basic build pipeline:

trigger:
- main

pool:
  vmImage: 'windows-latest'

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

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

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

This YAML configuration:

  • Triggers the build on changes to the main branch.
  • Uses the DotNetCoreCLI tasks to restore NuGet packages, build the solution, and execute tests.

Step 3. Create Release Pipeline (CD)

  1. Click on Releases: Inside your Azure DevOps project, go to Releases > New Pipeline.
  2. Choose a Template: Select an appropriate template or start with an empty job.
  3. Add Tasks: Configure stages, tasks, and environments based on your deployment requirements.

Here's a simple deployment stage configuration in YAML:

trigger:
- main

pool:
  vmImage: 'windows-latest'

stages:
- stage: 'Deploy'
  displayName: 'Deploy to Azure App Service'
  jobs:
  - job: 'Deploy'
    displayName: 'Deploy'
    steps:
    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: '<AzureSubscription>'
        appType: 'webApp'
        WebAppName: '<YourWebAppName>'
        packageForLinux: '$(Pipeline.Workspace)/drop/*.zip'

Replace <AzureSubscription> with your Azure subscription name and <YourWebAppName> with the name of your Azure Web App.

Step 4. Save and Run the Pipeline

Once you've defined both the CI and CD pipelines, save your changes and trigger a manual run or wait for the configured triggers to initiate the pipeline.

Conclusion

Creating a CI/CD pipeline for a .NET Web API application in Azure DevOps empowers developers to automate build, test, and deployment processes, ensuring consistent and efficient software delivery. Azure DevOps offers a flexible and robust environment to customize pipelines according to project requirements, ultimately enhancing productivity and software quality.