T Le
T Le

Reputation: 11

Azure Devops Pipeline stage progress via Rest API

I'm trying to retrieve the progress (stages?) from a pipeline, from an Azure DevOps Project, via the Azure Rest API. Several endpoints are already quite clear: retrieving projects within your organization, pipelines, the runs of each pipeline. However, the actual progress of each run, in more detail which step / stage the pipeline run is, I cannot find. https://learn.microsoft.com/en-us/rest/api/azure/devops/build/stages?view=azure-devops-rest-7.0 only returns: in progress, but not the exact detail i'm looking for.

Stages Progress: stages progress

https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/list?view=azure-devops-rest-7.0

https://learn.microsoft.com/en-us/rest/api/azure/devops/build/stages?view=azure-devops-rest-7.0

get me the progress of the stages, but unable to find

Upvotes: 1

Views: 1148

Answers (2)

fenrir
fenrir

Reputation: 383

You can use the timeline endpoint _apis/build/builds/{buildId}/timeline where the buildId is your pipeline run id aka build id.

The TimelineRecord provides you with very detailed information for example:

  • type - will tell you whether the record is for a stage or task,
  • name of the task/stage,
  • the result code or status code,
  • start and finish times,
  • attempts,
  • even the URIs to the log files produced by the steps etc.

Upvotes: 1

hacky643
hacky643

Reputation: 1

I've made a little PowerShell code to handle that case (not perfect but this is the logic behind this) This code

  • Check the status of the stage in the pipeline run

  • Run this stage for status ("succeeded", "canceled", "failed", "skipped", "notStarted", "partiallySucceeded")

    # Define variables
    $organization = "ORG_URL"
    $project = "PROJECT_NAME"
    $runId = "RUN_ID_NUMBER"       # Replace with your run ID
    $stageName = "build"  # Replace with the specific stage name
    $pat = "PAT_TOKEN" # Ensure this has Build permissions
    
    # Convert PAT to Base64
      $headers = @{
          Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
      }
    
      # API URL
      $uri = "$organization/$project/_apis/build/builds/$runId/timeline?api-version=7.1"
    
      # check the status of the stage
      $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
    
      $status = $response.records | Where-Object { $_.identifier -eq $stageName } | Select-Object -First 1 -ExpandProperty "state"
      write-host "Stage status: $status"
    
      # Status possibles are: "inProgress", "pending", "succeeded", "canceled", "failed", "skipped", "notStarted", "partiallySucceeded"
      if (($status -ne "inProgress") -and ($status -ne "pending")) { 
          write-host "Stage is not in progress or pending, no need to retry"
    
          $uri = "$organization/$project/_apis/build/builds/$runId/stages/$($stageName)?api-version=7.1"
          # Request Body
          $body = @{
              forceRetryAllJobs = $true
              state             = "retry"
          } | ConvertTo-Json -Depth 2
    
          # Trigger the stage
          $response = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $body -ContentType "application/json" 
    
          # Output response
          $response
          write-host "Retrying stage $stageName"
          write-host "check url: $organization/$project/_build/results?buildId=$runId&view=results"
      }
      else {
          write-host "Stage is in progress or pending, no need to retry"
      }
    

Upvotes: 0

Related Questions