Manually deploy to stages is not support in yaml pipeline currently. Please check this open issue.
You can try adding dependsOn and condition for each stage. For below example yaml pipeline. Stage Build will start to run only after stage Start successfully complete, Then Stage Build will wait for approval, Stage Release willnot be triggered until Stage Build is approved and successfully finished.
You can define the pr trigger and set autocancel=true
(the default is true)to cancel previous runs if new changes were pushed to the same pr.
The batch property for trigger
can achieve a similar effect. It will not start a new run if the current pr in still in building.
trigger:
batch: boolean # batch changes if true (the default); start a new build for every push if false
branches:
include:
_
pr:
autoCancel: true
branches:
include:
- master
stages:
- stage: Start
jobs:
- job: A
pool:
vmImage: windows-latest
steps:
- powershell: |
echo "i am job a"
- stage: Build
dependsOn: Start
condition: succeeded()
jobs:
- deployment: Dev
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-16.04'
# creates an environment if it doesn't exist
environment: 'Dev'
strategy:
# default deployment strategy, more coming...
runOnce:
deploy:
steps:
- script: echo "i am dev environment"
- stage: Release
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Environ
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-16.04'
# creates an environment if it doesn't exist
environment: 'Environment'
strategy:
# default deployment strategy, more coming...
runOnce:
deploy:
steps:
- script: echo "i am Environment environment"
Update: Cancel in progress builds via powershell scripts.
You can add a powershell task at the top of your pipeline to call build api. Below scripts get all the in progress builds and cancel them except current build.
- task: PowerShell@2
inputs:
targetType: inline
script: |
$header = @{ Authorization = "Bearer $(system.accesstoken)" }
$buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=5.1"
echo $buildsUrl
$builds = Invoke-RestMethod -Uri $buildsUrl -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)
{
echo $build.id
$build.status = "cancelling"
$body = $build | ConvertTo-Json -Depth 10
$urlToCancel = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$($build.id)?api-version=5.1"
echo $urlToCancel
Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header $header
}
In order your pipeline to have the permission to cancel the current running build. You need go to your pipeline, click on the 3dots and choose Manage security

Then set the Stop builds permission to Allow for user Project Collection Build Service(projectName),
