How to get the result of the latest/last pipeline build in Azure DevOps

119 Views Asked by At

In Azure DevOps, I am trying to get the result of the latest/last pipeline on a branch for a pipeline. This is to check if the previous build on the branch is failed due to some errors and after fixing the errors, the pipeline should trigger corresponding to the commit to the branch. If the branch has a successful build, the pipeline should skip the build.

I am trying to get result from the using below command to get the result using Azure DevOps API call which works fine in the browser but gives null when run through the pipeline.

previousRunId=$(curl -u ":$(System.AccessToken)" -X GET "$(System.CollectionUri)/$(System.TeamProjectId)/_apis/build/builds?definitions=$(System.DefinitionId)&branchName=$(Build.SourceBranch)&$top=1" | jq '.value[0].result')
echo "Previous run status: ${previousRunId}"

enter image description here

How to get the actual result as below?

enter image description here

1

There are 1 best solutions below

6
Shamrai Aleksander On

There is an exception in your script. You may find the following message If you run your command without jq:

{"$id":"1","innerException":null,"message":"The buildIds filter may not be used with other filter parameters.","typeName":"Microsoft.TeamFoundation.Build.WebApi.InvalidBuildQueryException, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"InvalidBuildQueryException","errorCode":0,"eventId":3000}

You have to escape $top

$(System.CollectionUri)/$(System.TeamProjectId)/_apis/build/builds?definitions=$(System.DefinitionId)&branchName=$(Build.SourceBranch)&\$top=1