There is no such feature in Azure DevOps.
The closest thing it's to use "Batching CI builds" - when a build is running, the system wait until the build is completed, then queues another build of all changes that have not yet been built.
To enable it in yaml build add this in the trigger
section:
batch: true
In the calssic editor, go to "Triggers" tab and mark the checkbox "Batch changes while a build is in progress".
Edit:
You can run a PowerShell script in the beginning of the build that cancel the running builds from the same definition:
$header = @{ Authorization = "Bearer $env:System_AccessToken" }
$buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/builds/builds"
$builds = Invoke-RestMethod -Uri $url -Method Get -Header $header
$buildsToStop = $builds.value.Where({ ($.status -eq 'inProgress') -and ($_.definition.name -eq $(Build.DefinitionName)) -and ($_.id -ne $(Build.BuildId)) })
ForEach($build in $buildsToStop)
{
$build.status = "Cancelling"
$body = $build | ConvertTo-Json -Depth 10
$urlToCancel = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(builds.id)"
Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header $header
}
I used OAuth token for authorization (enable it on the job options) and in inline script ($(varName)
and not $env:varName
).
Now, if you have one build that running and someone else trigger another build that started to run, in this step the first build will be canceled.