Search code examples
azure-devopsazure-devops-rest-api

How can I distinguish builds from different branches?


I am using the MS Azure DevOps REST API to download artifacts from the latest pipeline build from a given branch.

To do so, I first need to find out the build definition ID, which I do using the definitions GET endpoint (and then filtering the result by the pipeline name).

Once I have this, I can use the builds GET endpoint to fetch the latest successful builds for my build definition ID.

Now, my problem is this - the underlying pipeline is executed for different branches in my source repository. In the web-based DevOps UI, that looks like this for a given pipeline run:

pipeline run sources

In there, the first source repository/branch is the one where the pipeline YAML is hosted. That's always the same.

The second one is the one I'm actually interested in; it indicates which release branch the build was executed from.

Unfortunately, I cannot find this information in the Build data structure as returned from the builds endpoint; at least not in an intuitively typed and named field. There are two properties named repository and sourceBranch, but they only contain information about the first listed repo/branch from the DevOps web interface.

How can I distinguish builds from my different release branches when retrieving information via the public Azure DevOps REST API?


Solution

  • How can I distinguish builds from my different release branches when retrieving information via the public Azure DevOps REST API?

    You can use the Rest API: Runs - Get to get the Pipeline resource repo branch info.

    GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}?api-version=7.1-preview.1
    

    For example:

    resources:
      repositories:
        - repository: common
          type: git
          ref: main
          name: pipelines-javascript-docker2
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - checkout: common
    

    Pipeline UI

    enter image description here

    Rest API Result:

    enter image description here

    In this case, you can use Rest API to get the Build definition Id and the latest build id. Then you can use the Build definition id and the latest build ID in the Runs - Get Rest API.

    GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{BuildDefinitionID}/runs/{Latestbuildid}?api-version=7.1-preview.1
    

    Update:

    Powershell script sample:

    $token = "PAT"
    
    $url="https://dev.azure.com/{org}/{project}/_apis/pipelines/{definitionid}/runs?api-version=7.1-preview.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
    
    foreach($pipelineid in $response.value.id)
    {
     echo $pipelineid
     $urlgetinfo ="https://dev.azure.com/{org}/{project}/_apis/pipelines/{definitionid}/runs/$($pipelineid)?api-version=7.1-preview.1"
    
     $response1 = Invoke-RestMethod -Uri $urlgetinfo -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
    
     echo $response1.resources.repositories.common.refName
    }
    

    Result:

    enter image description here