gpt4 book ai didi

azure - 是否可以通过编程方式取消 Azure DevOps 管道作业?

转载 作者:行者123 更新时间:2023-12-03 16:07:19 24 4
gpt4 key购买 nike

因为可以停止 Azure DevOps 管道中的单个步骤:

echo "##vso[task.complete result=Succeeded;]DONE"

参见:https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md#task-logging-commands

是否也可以检查条件并根据条件停止整个管道运行或作业?

PS。我知道,您可以为作业设置条件,但在我的情况下,整个管道是一个作业,由于其他原因,将其拆分为多个作业是没有意义的。

最佳答案

您可以通过 REST API 取消构建:

PATCH https://dev.azure.com/atbagga/atbagga/_apis/build/Builds/120
Request content: {'status': 'Cancelling'}

这里有一个例子:

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

为此,您将得到该输出:

enter image description here

如果你遇到这样的错误:

 | {"$id":"1","innerException":null,"message":"Access denied.
| DevOps Manual Build Service (thecodemanual) needs Stop builds
| permissions for vstfs:///Build/Build/1611 in team project
| DevOps Manual to perform the
| action.","typeName":"Microsoft.TeamFoundation.Build.WebApi.AccessDeniedException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"AccessDeniedException","errorCode":0,"eventId":3000}

请确保您的构建帐户有权停止构建:

您将在此部分下找到此内容:

enter image description here

enter image description here

请注意

您不能将build设置为已完成。如果你这样做了。整个管道仍将被执行。因此,如果这不是您想要的,您需要使用之前在管道中设置的输出变量向每个步骤添加条件,并以这种方式忽略这些步骤。

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 - 是否可以通过编程方式取消 Azure DevOps 管道作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62044055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com