MSDN Community

Exploring .NET CI/CD with Azure DevOps Pipelines

Azure DevOps Pipelines for .NET CI/CD

Azure DevOps Pipelines is a powerful and flexible service that you can use to automate building, testing, and deploying your .NET applications. It integrates seamlessly with Azure DevOps Repos (or other Git repositories) and provides a rich set of features for creating robust Continuous Integration (CI) and Continuous Deployment (CD) workflows.

Understanding CI/CD in .NET

Before diving into Azure DevOps Pipelines, let's briefly recap the core concepts of CI/CD:

Setting Up Your First .NET Pipeline

Azure DevOps Pipelines uses YAML to define your pipelines, which offers several advantages:

Basic Build Pipeline Example

Here's a simple YAML snippet for a .NET build pipeline:


trigger:
- main

pool:
  vmImage: 'windows-latest'

steps:
- task: UseDotNet@2
  displayName: 'Use .NET SDK 6.0'
  inputs:
    packageType: 'sdk'
    version: '6.0.x'

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

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

- task: DotNetCoreCLI@2
  displayName: 'Run tests'
  inputs:
    command: 'test'
    projects: '**/*[Tt]ests/*.csproj'
    arguments: '--configuration Release'
            

Key Azure DevOps Pipeline Concepts

Tip: For complex .NET projects, consider using .NET SDK tasks to manage SDK versions and .NET Core CLI tasks for running commands like dotnet build, dotnet test, and dotnet publish.

Multi-Stage Pipelines for Deployment

Beyond building and testing, Azure DevOps Pipelines excels at deploying your .NET applications. You can define multiple stages to represent different environments (e.g., Dev, Staging, Production) with approval gates and automated deployments.

Example Deployment Stage


- stage: DeployToStaging
  displayName: 'Deploy to Staging'
  jobs:
  - deployment: DeployWebApp
    displayName: 'Deploy Web App'
    environment: 'Staging' # Link to an Azure DevOps Environment
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureRmWebAppDeployment@4
            displayName: 'Deploy Azure Web App'
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: 'Your Azure Service Connection'
              appType: 'webAppLinux'
              appName: 'your-staging-webapp-name'
              package: '$(Pipeline.Workspace)/drop/**/*.zip' # Path to your published artifact
            

Best Practices for .NET CI/CD with Azure DevOps

Mastering Azure DevOps Pipelines can significantly improve the efficiency, reliability, and speed of your .NET application development lifecycle. Explore the documentation for more advanced scenarios like integration with Azure Kubernetes Service (AKS), serverless functions, and more.

Learn More on Microsoft Docs