问题描述
因为可以在 Azure DevOps 管道中停止单个步骤:
echo "##vso[task.complete result=Succeeded;]DONE"
参见:
如果你遇到这样的错误:
|{"$id":"1","innerException":null,"message":"访问被拒绝.|DevOps 手动构建服务(代码手册)需要停止构建|团队项目中 vstfs:///Build/Build/1611 的权限|DevOps 手册来执行|action.","typeName":"Microsoft.TeamFoundation.Build.WebApi.AccessDeniedException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"AccessDeniedException","errorCode":0,"eventId":3000}
请确保您的构建帐户有权停止构建:
您将在此部分下找到:
请注意
您不能将构建设置为已完成.如果你这样做了.整个管道仍将执行.因此,如果这不是您想要的,您需要使用先前在管道中设置的输出变量向每个步骤添加条件,并以这种方式忽略这些步骤.
步骤:- 任务:PowerShell@2名称:条件步骤输入:目标类型:'内联'脚本: |写主机我在这里"写主机('$(SomeVariable)' -eq '停止')if ('$(SomeVariable)' -eq '停止') {写主机'##vso[task.setvariable variable=shouldStop;isOutput=true]Yes'}写主持人现在在这里!"密码:真的- pwsh:开始-睡眠-秒 60条件:ne(变量['ConditionalStep.shouldStop'],'是')- 任务:PowerShell@2条件:ne(变量['ConditionalStep.shouldStop'],'是')输入:目标类型:'内联'脚本: |$uri = "https://dev.azure.com/thecodemanual/DevOps Manual/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"写主机 $uri# 调用 REST 调用$build = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $(System.AccessToken)"} -ContentType "application/json"$taskResult = $build.records |Where-Object {$_.name -eq "ConditionalStep" } |选择对象结果写主机 $taskResult.result密码:真的
As it is possible to stop a single step in a Azure DevOps pipeline:
echo "##vso[task.complete result=Succeeded;]DONE"
Is it also possible to check a condition and stop the whole pipeline run or job depending on that?
PS. I know, you can set conditions to jobs, but in my case the whole pipeline is a single job and it does not make sense to split it into multiple jobs, because of other reasons.
You can cancel a build through REST API:
PATCH https://dev.azure.com/atbagga/atbagga/_apis/build/Builds/120
Request content: {'status': 'Cancelling'}
Here you have an example:
steps:
- task: PowerShell@2
name: ConditionalStep
inputs:
targetType: 'inline'
script: |
Write-Host "I'm here"
Write-Host ('$(SomeVariable)' -eq 'Stop')
if ('$(SomeVariable)' -eq 'Stop') {
$uri = "https://dev.azure.com/thecodemanual/DevOps Manual/_apis/build/builds/$(Build.BuildId)?api-version=5.1"
$json = @{status="Cancelling"} | ConvertTo-Json -Compress
$build = Invoke-RestMethod -Uri $uri -Method Patch -Headers @{Authorization = "Bearer $(System.AccessToken)"} -ContentType "application/json" -Body $json
Write-Host $build
}
Write-Host "And now here!"
pwsh: true
- pwsh: Start-Sleep -Seconds 60
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$uri = "https://dev.azure.com/thecodemanual/DevOps Manual/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"
Write-Host $uri
# Invoke the REST call
$build = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $(System.AccessToken)"} -ContentType "application/json"
$taskResult = $build.records | Where-Object {$_.name -eq "ConditionalStep" } | Select-Object result
Write-Host $taskResult.result
pwsh: true
For that you will get that output:
If you get such error:
Please make sure that your build account has permission to stop a build:
You will find this under this section:
Please note
What you can't do is set a build as completed. If you dod this. Whole pipeline will be still executed. So if this isn't what you want, you need to add condition to every step with an output variable set previously in the pipeline and in that way ignore those steps.
steps:
- task: PowerShell@2
name: ConditionalStep
inputs:
targetType: 'inline'
script: |
Write-Host "I'm here"
Write-Host ('$(SomeVariable)' -eq 'Stop')
if ('$(SomeVariable)' -eq 'Stop') {
Write-Host '##vso[task.setvariable variable=shouldStop;isOutput=true]Yes'
}
Write-Host "And now here!"
pwsh: true
- pwsh: Start-Sleep -Seconds 60
condition: ne(variables['ConditionalStep.shouldStop'], 'Yes')
- task: PowerShell@2
condition: ne(variables['ConditionalStep.shouldStop'], 'Yes')
inputs:
targetType: 'inline'
script: |
$uri = "https://dev.azure.com/thecodemanual/DevOps Manual/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"
Write-Host $uri
# Invoke the REST call
$build = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $(System.AccessToken)"} -ContentType "application/json"
$taskResult = $build.records | Where-Object {$_.name -eq "ConditionalStep" } | Select-Object result
Write-Host $taskResult.result
pwsh: true
这篇关于是否可以以编程方式取消 Azure DevOps 管道作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!