简体   繁体   English

从另一个管道触发 Azure DevOps 管道

[英]Trigger Azure DevOps pipeline from another pipeline

I was looking at the azure triggers documentation and still not able to find an appropriate solution.我正在查看 azure triggers 文档,但仍然无法找到合适的解决方案。 How during the execution of pipeline 1 can you trigger pipeline 2, wait for it to successfully finish or fail, and based on pipeline 2 results either continue execution of pipeline 1 or fail?如何在流水线 1 的执行过程中触发流水线 2,等待它成功完成或失败,并根据流水线 2 的结果继续执行流水线 1 或失败?

How during the execution of pipeline 1 can you trigger pipeline 2, wait for it to successfully finish or fail, and based on pipeline 2 results either continue execution of pipeline 1 or fail?如何在管道 1 执行期间触发管道 2,等待它成功完成或失败,并根据管道 2 结果继续执行管道 1 或失败?

Trigger one pipeline after another, it will run your pipeline upon the successful completion of the triggering pipeline.触发一个又一个管道,它会在触发管道成功完成后运行您的管道。 We cannot use it to trigger pipeline 1 in the execution of pipeline 1.我们不能在管道 1 的执行中使用它来触发管道 1。

As a workaround:作为解决方法:

a.一种。 We can add task power shell and add script to call the REST API to queue the build.我们可以添加任务电源外壳并添加脚本来调用 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.Add task power shell and enter the code Start-Sleep -Seconds 1000 to sleep the pipeline 1添加任务电源外壳并输入代码Start-Sleep -Seconds 1000使管道1休眠

c. C。 Add the task power shell in the pipeline 1 to get the pipeline 2 build result via the REST API, and set the result as env variable.在管道1中添加任务power shell,通过REST API获取管道2的构建结果并将结果设置为env变量。

d. d. Configure the condition in the next task to check the env variable value.在下一个任务中配置条件以检查 env 变量值。 If the value is succeeded , continue run the pipeline 1如果该值succeeded ,则继续运行管道 1

You are probably looking for something like this.你可能正在寻找这样的东西。

# 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

its right there in the link, you have linked, but in the sibling section of the docs.它就在链接中,您已链接,但在文档的兄弟部分。 I am surprised you missed it.我很惊讶你错过了它。

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

So here is my solution based on the suggestion above:所以这是我基于上述建议的解决方案:

- 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
        }

parameter for pipeline id: pipelineId: $(resources.pipeline.resource.pipelineId)管道 id 参数: pipelineId: $(resources.pipeline.resource.pipelineId)

If you're ok with using extensions, the Trigger Build Task you can get in the marketplace should support all of your requirements.如果您可以使用扩展,那么您可以在市场上获得的触发器构建任务应该支持您的所有需求。

It lets you trigger another pipeline, with an option to wait for it, and options about how to treat failures of that pipeline if you do wait.它允许您触发另一个管道,并提供等待它的选项,以及有关如何在等待时处理该管道故障的选项。 So you can use that to trigger a build, wait for it, and succeed / fail based on if the build succeeds / fails.所以你可以使用它来触发构建,等待它,并根据构建是否成功/失败来成功/失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM