1

I have the code below that runs 3 pipelines but I want to make it so it runs the first one and builds the product and then it runs the other two instead of all of them running at once so do the first one and then do the other 2 after the first one was successful.

variables:
- group: ReleaseVariables

name: 5.8$(rev:.r)

jobs:
- job: Ring_Web_Policy_Editor
  timeoutInMinutes: 360

  pool:
    name: DATA-AUTOMATION-WIN10
    demands: Cmd

  steps:
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: 'Web Policy Editor'
      Branch: '$(Build.SourceBranch)'
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: '(Chrome) Web Policy Editor Automation'
      Branch: '$(Build.SourceBranch)'
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: '(Firefox) Web Policy Editor Automation'
      Branch: '$(Build.SourceBranch)'
    

2 Answers2

0

do the first one and then do the other 2 after the first one was successful.

You could add a PowerShell task after the first Trigger Pipeline Task.

Here is Powershell script sample:

$token = "PAT"

$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions/{DeefinitionId}?includeLatestBuilds=true&api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))



$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

$buildid = $response.latestBuild.id

$success = $false

do{
    try{
    $Buildurl2 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$($buildid)?api-version=5.0"
    $Buildinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Buildurl2 -Headers @{Authorization=("Basic {0}" -f $token)}
    $BuildStatus= $Buildinfo2.status 
    $result = $Buildinfo2.result
    echo $result
    echo $BuildStatus
 
   
        if($BuildStatus -eq "completed") {            

            write-output "No Running Pipeline, starting Next Pipeline"
            $success = $true 
                       
      } else {   
            Write-output "Pipeline Build In Progress, Waiting for it to finish!"  
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30         

            }
    
      
    }
    catch{
        Write-output "catch - Next attempt in 30 seconds"
        write-output "1"
        Start-sleep -Seconds 30
      # Put the start-sleep in the catch statemtnt so we
      # don't sleep if the condition is true and waste time
    }
    
    $count++
    
}until($count -eq 2000 -or $success -eq $true )
if ($result -ne "succeeded")
{
   echo "##vso[task.logissue type=error]Something went very wrong."
}

if(-not($success)){exit}

Explanation:

This powershell script runs the following two Rest APIs:

Definitions - Get

Builds - Get

The script checks the status of the pipeline(by polling) that has been triggered . If the pipeline is completed and the result is successful, it will trigger the other two pipelines. Or it will wait for the pipeline finishing the build.

Pipeline sample:

 steps:
  - task: TriggerPipeline@1
  - task: PowerShell@2
  - task: TriggerPipeline@1
  - task: TriggerPipeline@1

Result Sample:

enter image description here

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Hi, I tried this and it just ran infinitely https://pastebin.com/xYYU3AGh I assume it couldn't pick up the build – Mohammed Faisal Qureshi Dec 23 '20 at 09:36
  • @MohammedFaisalQureshi For ran infinitely, Can you explain this to me? Are you stuck in this powershell task? If possible, you can share the screenshot with me. – Kevin Lu-MSFT Dec 23 '20 at 09:38
  • Based on my test, if you use this code, it will poll the pipeline status every 30 seconds until the pipeline runs finishing. Then it will run the following tasks. – Kevin Lu-MSFT Dec 23 '20 at 09:42
  • It just runs that next attempt in 30 seconds in progress and runs forever even when the build as finished https://prnt.sc/w8dxlf – Mohammed Faisal Qureshi Dec 23 '20 at 09:45
  • Thanks. According to your screenshot, the apis seems to hasn't run successfully. Because the echo command hasn't output the value. Can you try to hard coding PAT? For example : `$token = "PAT"`. (without variable) And please make sure that you have input the correct build definition. – Kevin Lu-MSFT Dec 23 '20 at 09:53
  • Yup build def is 528 and I just get the same loop without the echos above when I manually put in the token – Mohammed Faisal Qureshi Dec 23 '20 at 10:00
  • Additional information: you need to ensure that the pipeline can run in parallel. For example: the pipeline 1 task trigger the pipeline 2 , the pipeline 1 and pipeline 2 could run at the same time. If you cannot run parallel jobs, you will get stuck. In this case, I could reproduce this issue. You may try to set the project as Public and check if it could work. – Kevin Lu-MSFT Dec 23 '20 at 10:06
  • They can all run separately so I can run 3 Task 1 builds if I wanted to an Automation can be run 3 tasks so 1 chrome and 1 firefox and 1 free for something else. So I guess it can run parallel – Mohammed Faisal Qureshi Dec 23 '20 at 10:08
  • I also can't make the project public sadly – Mohammed Faisal Qureshi Dec 23 '20 at 10:11
0
            $Username=${env:USERNAME}
            $PAT=${env:PATTOKEN}
            $Buildurl2 = "https://dev.azure.com/{ORGANISATION}/{Project}/_apis/build/builds/$($buildid)?api-version=5.0"
            $token = -join("$Username", ":", "$PAT")
            $headers = @{ 
                Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
                'Content-Type' = "application/json"
            }
            $Buildinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Buildurl2 -Headers $headers
            $BuildStatus= $Buildinfo2.status 
            $result = $Buildinfo2.result 
            echo $result
            echo $BuildStatus

The answer above is correct but I had to add this to authenticate mine.