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

Is it possible to cancel a Azure DevOps pipeline Job programmatically?

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

为此你将得到输出:

如果出现这样的错误:

 | {"$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}

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

您将在本节下找到:

请注意

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

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