Using Azure DevOps for CI/CD with .NET
Introduction
Azure DevOps provides a set of services for managing the lifecycle of software projects, including CI/CD pipelines. In this tutorial, we will walk through setting up a CI/CD pipeline for a .NET application using Azure DevOps.
Setting Up Azure DevOps
First, you need to set up an Azure DevOps organization and project.
Steps to Create an Azure DevOps Organization and Project
// Step 1: Go to the Azure DevOps portal: https://dev.azure.com/
// Step 2: Click on 'New organization' and follow the prompts to create an organization
// Step 3: Create a new project within the organization
Creating a Git Repository
Next, create a Git repository in Azure DevOps and push your .NET code to it.
Steps to Create a Git Repository
// Step 1: Navigate to Repos > Files in your Azure DevOps project
// Step 2: Click on 'Initialize' to create a new repository
// Step 3: Follow the instructions to push your existing code to the new repository
Setting Up a Build Pipeline
Now, set up a build pipeline to automate the build process for your .NET application.
Steps to Create a Build Pipeline
// Step 1: Navigate to Pipelines > Pipelines in your Azure DevOps project
// Step 2: Click on 'New pipeline' and follow the prompts
// Step 3: Select your repository and choose 'YAML' as the pipeline configuration option
// Step 4: Create a YAML file for your build pipeline:
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '5.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: |
dotnet build --configuration Release
displayName: 'Build project'
Running Tests in the Build Pipeline
It's a good practice to include tests in your build pipeline. Add the following steps to run tests.
Adding Test Steps to the Pipeline
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '5.x'
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: |
dotnet build --configuration Release
displayName: 'Build project'
- script: |
dotnet test --configuration Release
displayName: 'Run tests'
Setting Up a Release Pipeline
Next, create a release pipeline to deploy your application.
Steps to Create a Release Pipeline
// Step 1: Navigate to Pipelines > Releases in your Azure DevOps project
// Step 2: Click on 'New pipeline' and select your build pipeline as the artifact
// Step 3: Add stages to your release pipeline for each environment (e.g., Dev, QA, Production)
Deploying to Azure App Service
Configure the release pipeline to deploy to Azure App Service.
Steps to Configure Deployment
// Step 1: Add an Azure App Service Deployment task to each stage in your release pipeline
// Step 2: Configure the task with your Azure subscription and App Service details
// Step 3: Save and create a release to deploy your application
Monitoring and Managing Pipelines
Azure DevOps provides various tools to monitor and manage your pipelines.
Monitoring Pipelines
// Step 1: Navigate to Pipelines > Pipelines or Pipelines > Releases to view the status of your builds and releases
// Step 2: Use the logs and history features to troubleshoot issues and track changes
Conclusion
In this tutorial, you learned how to use Azure DevOps for CI/CD with a .NET application, including setting up your environment, creating a build and release pipeline, and deploying to Azure App Service.