【问题标题】:Trigger Azure DevOps pipeline from another pipeline从另一个管道触发 Azure DevOps 管道
【发布时间】:2021-01-13 15:20:33
【问题描述】:

我正在查看 azure triggers documentation,但仍然无法找到合适的解决方案。 如何在流水线 1 的执行过程中触发流水线 2,等待其成功完成或失败,并根据流水线 2 的结果继续执行流水线 1 或失败?

【问题讨论】:

    标签: azure azure-devops azure-pipelines devops azure-pipelines-release-pipeline


    【解决方案1】:

    您可能正在寻找类似的东西。

    # this is being defined in app-ci pipeline
    resources:
      pipelines:
      - pipeline: securitylib   # Name of the pipeline resource
        source: security-lib-ci # Name of the pipeline referenced by the pipeline resource
        trigger: 
          branches:
          - releases/*
          - master
    

    它就在链接中,您已链接,但在文档的兄弟部分。我很惊讶你错过了它。

    https://docs.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops&tabs=yaml

    【讨论】:

    • 感谢杰伊的回复。我查看了那个选项,但是这个解决方案不会停止当前的管道执行,只会在执行完成后触发第二个管道。来自文档:“在成功完成触发管道后触发运行您的管道。我需要停止管道 A 直到管道执行 B 完成。然后根据管道 B 的结果,继续执行管道 A 或停下来。
    • 啊,对,当然,我假设有两个管道场景,文章也是如此。问题是,据我了解,YAML 不是 javascript。它更像是一条从山上流下来的河流。您可以路由事物,但不能环回。如果我是你,我会建造 3 条管道。管道 1 触发管道 2,然后,根据管道 2 的结果,我触发下一个管道 3(你的第一个管道,如果你有更多的事情要做,分解为管道 1 和 3 或更多部分)跨度>
    【解决方案2】:

    在管道 1 执行过程中如何触发管道 2,等待它成功完成或失败,并根据管道 2 的结果继续执行管道 1 或失败?

    触发一个又一个的管道,它会在触发管道成功完成后运行你的管道。在管道 1 的执行过程中,我们不能使用它来触发管道 1。

    作为一种解决方法:

    一个。我们可以添加任务 power shell 并添加脚本来调用 REST API 来对构建进行排队。

    $connectionToken="PAT"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $PipelineUrl = "https://dev.azure.com/{Org name}/{project name}/_apis/pipelines/{Pipeline ID}/runs?api-version=6.0-preview.1" 
    
    $body ="{ 
     `"resources`":{
            `"repositories`":{
                `"self`":{`"refName`":`"refs/heads/master`"
                }
             }
        }
    }"
    $Pipelines = Invoke-RestMethod -Uri $PipelineUrl -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
    

    b.添加任务power shell并输入代码Start-Sleep -Seconds 1000休眠管道1

    c。在管道 1 中添加任务 power shell,通过REST API, 获取管道 2 构建结果并将结果设置为 env 变量。

    d。在下一个任务中配置 condition 以检查 env 变量值。如果值为succeeded,则继续运行管道1

    【讨论】:

      【解决方案3】:

      所以这是我基于上述建议的解决方案:

      - task: PowerShell@2
        displayName: Running second pipeline
        inputs:
             targetType: 'inline'
             script: |
              Write-Host "Triggering pipeline..."
              $connectionToken= "$(PAT)"
      
              $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
              $PipelineUrl = "https://dev.azure.com/YourOrganization/yourProject/_apis/pipelines/${{ parameters.pipelineId }}/runs?api-version=6.0-preview.1" 
              Write-Host "Pipeline url: $PipelineUrl"
              $body ="{ 
               `"resources`":{
                      `"repositories`":{
                          `"self`":{`"refName`":`"refs/heads/${{ parameters.branch }}`"
                          }
                       }
                  }
              }"       
              $response = Invoke-RestMethod -Uri $PipelineUrl -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
              Write-Host "Response: $response"
              $BuildUrl = "https://dev.azure.com/YourOrganization/yourProject/_apis/build/builds/$($response.Id)?api-version=6.1-preview.6"
              Write-Host  $BuildUrl
              
              $TimeoutAfter = New-TimeSpan -Minutes 15
              $WaitBetweenPolling = New-TimeSpan -Seconds 10
              $Timeout = (Get-Date).Add($TimeoutAfter)
              do
              {
              $Response = Invoke-RestMethod -Uri $BuildUrl -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
              Write-Host $Response.status
              Start-Sleep -Seconds $WaitBetweenPolling.Seconds
              }
              while ((($Response.status -eq "notStarted") -or ($Response.status -eq "inProgress")) -and ((Get-Date) -lt $Timeout))
              
              if ($Response.result -ne "succeeded")
              {
                  Write-Host $Response.result
                  exit 1
              }
      

      管道ID参数:pipelineId: $(resources.pipeline.resource.pipelineId)

      【讨论】:

        【解决方案4】:

        如果您可以使用扩展程序,那么您可以在市场上获得的 Trigger Build Task 应该支持您的所有要求。

        它允许您触发另一个管道,并提供等待它的选项,以及有关在等待时如何处理该管道故障的选项。因此,您可以使用它来触发构建,等待它,并根据构建是否成功/失败而成功/失败。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-07-01
          • 1970-01-01
          • 2020-04-22
          • 2020-10-13
          • 1970-01-01
          • 2020-04-13
          • 2020-06-14
          • 1970-01-01
          相关资源
          最近更新 更多