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

标签 azure azure-devops azure-pipelines azure-pipelines-yaml

因为可以停止 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/

相关文章:

azure-devops - 仅当Azure Pipelines中指定文件夹上没有更改时才触发生成

Azure 可用性集或区域虚拟机自动打开

json - 在Python中的azure函数应用程序中未在 `req.get_json()`中获取json输入

python - 使用 python 将 CSV 文件上传到 Microsoft Azure 存储帐户

node.js - 如何将 Azure DevOps 构建管道连接到专用终结点

nuget - Azure DevOps,在使用 website.sln 文件时恢复 "Visual Studio Build"的 NuGet 包

c# - 将 utc 转换为本地日期时间

templates - Azure DevOps 模板管道函数

Azure 机器人和网络聊天 : There was an error sending this message to your bot

azure - 从 AzureDevOps 替换 token 在应用程序设置中设置 JSON key