Azure DevOps Pipelines - How do I get the list of all tasks in a stage?

Viewed 116

As part of a validater script that needs to be run as part of a pipeline, I want to find the list of tasks that will be executed as part of the current stage.

Something like a pipeline variable or something that I can use to check the existence of a set of tasks in the pipeline.

1 Answers

According to your description, there is currently no out-of-the-box option to achieve this.

You can try to list the build timeline and get the tasks in the sepeific stage.

Powershell like:

#Create a PAT in your organization
$AzureDevOpsPAT = "{PAT}"
$header = $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
 
$organization = "{org}"
$project = "{project}"
$buildId ="{buildId}"

#get records.
$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId/timeline?api-version=6.0"
$response = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header

$taskId 
$stageId
$Id
$response.records | ForEach-Object {  

       if($_.name -eq "{jobname}" -and $_.type -eq "Phase"){
          $stageId = $_.parentId
          #Write-Host $jobId
       }
   
       if($_.name -eq "{stagename}"){
          $StageId = $_.id
          #Write-Host $StageId
       }

}
Write-Host $taskId
Write-Host $stageId

$response.records | ForEach-Object {  

if($taskId -eq $_.parentId){
  $Id = $_.id
}

}

$response.records | ForEach-Object {  

if($Id -eq $_.parentId){
  #task name and state
  Write-Host $_.name
  Write-Host $_.state
}

}
Related