Reputation: 11
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:
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
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:
Upvotes: 1
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