RobC
RobC

Reputation: 1415

How do I call Azure DevOps API for latest successful pipeline run?

I'm trying to accomplish what's described here by fearnight as a means of getting the commit ID of the most recent successful pipeline run, but I'm having trouble with the API call itself.

The API endpoint is described here and looks like this: GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definition}?api-version=6.0-preview.1

The problem I'm having with it is that I don't know what to use for the "definition" segment. So I figured I could list out some information about my builds using this call. I attempted this using code like this in my pipeline:

- task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        $uri="https://dev.azure.com/mycompany/myproject/_apis/build/builds?resultFilter=succeeded&api-version=6.1"
        $builds=(Invoke-RestMethod -Uri $uri -Method GET -Headers @{ Authorization = "Bearer $(System.AccessToken)" })
        echo "Response: $builds"
        echo "Builds: $builds.Build[0].definition.name"
        # echo "name: $builds[0].definition.name"
        # echo "definition ID: $builds[0].definition.id"
        # $builds | ForEach-Object {
        #   echo "Build definition name: $_.definition.name"
        # }

I must be coding these calls incorrectly, though, as this is typical of the responses I get:

========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/2ec4dfbd-d2d6-42e4-bd9e-9dfa3bad28f0.ps1'
Response: @{count=1000; value=System.Object[]}
Builds: @{count=1000; value=System.Object[]}.Build[0].definition.name
Finishing: PowerShell

To reframe my question, I need help with the syntax for querying the ADO API, which in turn I need to do for the purpose of getting the commit ID of the last successful run for my pipeline.

Can someone please help me out?

EDIT I've got the endpoint call working (I think) now, as I'm getting a response back, but I can't figure out how to access the values in the response from within my pipeline PS script. I'd think that to access one of these properties (e.g., sourceVersion, which is the one I need), I could do something like one of these:

$response=(Invoke-RestMethod -Uri $uri -Method GET -Headers @{ Authorization = "Bearer $(System.AccessToken)" })
    echo "Response: $response"
    echo "Source version: $response.value"
    # echo "Source version: $response.Build.sourceVersion"

All these give me, though is:

Response: @{_links=; properties=; tags=System.Object[] ... sourceVersion=fb29c721eaaaacf47f35ff900fe7086084cd321356;

How do I access and use the sourceVersion value?

Upvotes: 2

Views: 2345

Answers (2)

Doris Lv
Doris Lv

Reputation: 3398

Please use the pipeline's definition ID as the variable of "definition" segment like below: https://dev.azure.com/{organization}/{project}/_apis/build/latest/138?api-version=6.0-preview.1

You can find the definition id in the url of your pipeline: enter image description here

Upvotes: 1

KevinLee
KevinLee

Reputation: 324

Okay to get the commit ID associated with your latest pipeline run, refer to this SO answer: https://stackoverflow.com/a/60500333/13761014

I'll keep it easy for others to follow, by adding the API here below:
GET https://dev.azure.com/{org name}/{project name}/_traceability/runview/changes?currentRunId={build id}&__rt=fps&__ver=2

To get the latest build id, refer to this API: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/latest/get?view=azure-devops-rest-6.0

Once you have the response from the first API, you'll need to query for the key
"ms.vss-traceability-web.traceability-run-changes-data-provider"

This will give you the ID of the repositories and the commit IDs associated to the build.

Hope this is what you were looking for.

Upvotes: 1

Related Questions