目前,yaml 管道不支持手动部署到阶段。请检查此open issue。
您可以尝试为每个阶段添加dependsOn 和condition。对于以下示例 yaml 管道。 Stage Build 将在Stage Start 成功完成后开始运行,然后Stage Build 将等待批准,Stage Release 将在Stage Build 获得批准并成功完成后触发。
您可以定义pr trigger 并设置autocancel=true(默认为true)以取消之前的运行,如果新的更改被推送到同一个pr。
trigger 的batch 属性可以达到类似的效果。如果当前 pr 仍在构建中,它将不会开始新的运行。
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"
更新:Cancel in progress builds via powershell scripts。
您可以在管道顶部添加一个 powershell 任务来调用build api。下面的脚本获取所有正在进行的构建并取消它们,但当前构建除外。
- 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
}
为了让您的管道有权取消当前正在运行的构建。您需要转到您的管道,单击 3dots 并选择 Manage security
然后将停止构建的权限设置为允许用户Project Collection Build Service(projectName),