Azure Devops:代理在不同构建之间跳跃

Azure Devops: Agent Jumping Around Different Builds

我注意到每当我将阶段或作业添加到我的管道中并在队列中有多个构建时,就会发生这样的情况:image

以图片为例:

Jobs 似乎在尝试 运行 伪并行格式,但我只有 1 个构建代理。这导致超长的构建时间,因为它为每个工作做一个工作,有效地使第一个完成所需的时间加倍。有什么方法可以关闭它,让管道在移动到下一个之前完全完成一个构建?

谢谢

经过测试,我们发现 multi-stages yaml pipeline, that's why you encounter this issue. However, if you use release pipeline, there is Deployment queue settings 在不同版本中触发阶段的顺序不受控制,因此您可以指定“Deploy all in sequence”。

此外,我们发现了这个 ,它提供了一种开发自定义扩展的方法,使管道在进入下一个之前完全完成一个构建。

顺便说一句,您可以使用 PowerShell 任务取消其他 notStarted 构建,这是每个阶段的最后一步,因此后面的构建将被取消,然后当前构建可以完全完成。以下是示例PowerShell脚本,您需要create a PAT with full access.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |   
      Write-Host "Start checking."
      $url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitions={definitionId}&statusFilter=inProgress,notStarted&api-version=6.0"
      $connectionToken="PAT here"
      $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
      do
      {
          $pipelines = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
          $count = $pipelines.count
          if ($count -gt 1)
          {
              $lastestBuildId = $pipelines.value[-1].id
              $urlCancel = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/" + $lastestBuildId + "?api-version=6.0"
              $body = @{status="cancelling"} | ConvertTo-Json -Compress
              Write-Host $urlCancel
              $result= Invoke-RestMethod -Uri $urlCancel -Body $body -ContentType "application/json" -Headers @{authorization = "Basic $base64AuthInfo"} -Method Patch #cancel the latter noStarted builds
          }
      } while($count -gt 1)
      Write-Host "It's my turn now."

另一种解决方法是您可以 create an environment named approval-check and define approvals and checks 它,然后在 yaml 管道中添加以下 check 阶段作为第一阶段,因此必须批准新构建才能开始。

- stage: check
  jobs:
  - deployment: DeployWeb
    displayName: deploy Web App
    pool:
      vmImage: 'Ubuntu-latest'
    # creates an environment if it doesn't exist
    environment: 'approval-check'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Hello world