Azure DevOps Pull Request build validation pipeline for .Net 5 project

I'm a software consultant who loves to build projects and share my learnings on this blog.
Search for a command to run...

I'm a software consultant who loves to build projects and share my learnings on this blog.
Andrew Lindeman I am not sure of why that is happening. Ideally multiple PR validations can be triggered in parallel, provided the build agent has parallel jobs configured.
Can you please confirm my understanding about your problem :
feature-branch-1 to master. feature-branch-2 to master.Hi! Thanks for responding. To be clear, this is a on premises Azure DevOps server. When I google "parallel jobs", I found some links referring to hosted Azure DevOps.
But yes, the scenario you list is indeed the scenario that is causing my developers headaches.
Thanks!
I have a validation build configured for Pull Requests. When a build is ongoing due to a PR #1 and another developer creates PR #2, the build from PR #1 is cancelled mid build. This is driving my devs nuts. Do you know of a way to stop this (allow the build from PR #1 to complete before starting PR #2's build.) Many thanks!
Azure Functions is a powerful serverless compute platform, and Azure App Service handles a lot of the underlying configuration for you out of the box. However, there are scenarios where you may want t

If you’ve been exploring Azure AI learning paths or watching older demos, you’ve probably run into something confusing: The documentation and training links often reference Foundry (Classic) But the portal you’re using today might show Foundry (New...

In today's digital age, speed is paramount for web and mobile applications. Users expect instantaneous responses, and any delay can lead to a poor user experience and loss of engagement. Therefore, as developers, it's crucial to build web APIs that a...

Loops are fundamental constructs in any programming language. But have you ever wondered if there are any performance differences between them? For small collections, the difference might be negligible, but what about handling a million or 10 million...

When using an Azure Function app, it is necessary to connect it to a storage account, often referred to as Host storage. This storage is utilized by the Functions runtime, as well as by various triggers and bindings to coordinate between multiple run...

When working in teams, we always want to ensure that any new PR (Pull Request) doesn't break the main build pipeline, deploy broken code or cause unit test cases to fail. We never want the PR to be merged to the master/main branch and then revert it back once the build pipeline fails, instead we always prefer a proactive approach of preventing the PR from merging if the build is failing.
This article covers creating a YAML based PR build validation pipeline for the .Net 5 project and setting build policies in Azure DevOps to ensure all PRs to the master branch are automatically validated using the pipeline and only build validated PRs can be merged.
We will be using a sample Azure DevOps repo having the Getting started ASP.NET Core Web App project running on .Net 5 checked in. The same set of steps can be used for other .Net 5 projects as well.
This article will perform the following on the Azure DevOps project:
The first step is to set up a PR build validation yaml pipeline. I usually prefer to keep the pull request pipelines in the deployment\devops-pipelines\pull-requests folder from the root of the repository.
Create a WebApplication-pr.yml file (any file name that suits your project) in the deployment\devops-pipelines\pull-requests folder with the following content to create the PR pipeline which restores, builds, and tests the .Net 5 project code.
# File: WebApplication-pr.yml
# Description:
# PR build pipeline for WebApplication solution.
# This job performs the following activities:
# - Restores
# - Builds
# - Tests projects & collects code coverage
trigger: none
variables:
# Working Directory
workingDirectory: '$(System.DefaultWorkingDirectory)/'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
name: 'Azure Pipelines'
vmImage: 'windows-latest'
steps:
# Install .Net 5.0
- task: UseDotNet@2
displayName: 'Install .NET 5.0 SDK'
inputs:
packageType: 'sdk'
version: '5.0.x'
# Restore dependencies
- task: DotNetCoreCLI@2
displayName: 'Restore NuGet packages'
inputs:
command: 'restore'
projects: '**\WebApplication.sln'
feedsToUse: 'select'
includeNuGetOrg: true
noCache: false
# Build the solution
- task: DotNetCoreCLI@2
displayName: 'Build solution file'
inputs:
command: 'build'
projects: '**\WebApplication.sln'
arguments: '--configuration $(BuildConfiguration)'
# Run the tests and collect code coverage
- task: DotNetCoreCLI@2
displayName: 'Test .Net Core Project in code'
inputs:
command: 'test'
projects: |
src\Tests\**\*.csproj
publishTestResults: true
arguments: '--configuration $(BuildConfiguration) --collect:"Code coverage"'
The pipeline does 4 tasks in the Build stage - i.e.,
This is the bare minimum PR pipeline, but you could add in other tasks as required.
The above DevOps pipeline doesn't have any trigger defined as we will use the Azure DevOps Build Validation policies for triggering it. Also, the agent being used is a Microsoft Hosted Agent, in case your choice of agent is different, please change the pool in the build job.
Check-in the above pipeline to your code repository, and let's trigger it from Azure DevOps so that we can test the pipeline.

To add the above checked-in yaml pipeline to Azure DevOps pipelines,






Note: Please run the PR pipeline at least once before proceeding to the next step, as only in that case the pipeline will be visible on the Build Validation policy settings page.
Now that you have the PR pipeline ready and executed at least once, it's time to update the build validation policy for the master/main branch so that subsequent PRs will result in the pipeline being executed and completed successfully for the PR to be merged.
To add the build validation policy,



Create a Pull Request and the pipeline should get triggered automatically as part of the 'Required' checks.

In case of build-failure, the Pull Request won't be allowed to merge.

That's it!! Azure DevOps Pull Request build validation pipeline has been set up for .Net 5 project and this policy plus pipeline will ensure only successfully building code is merged to master/main branch.