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?