azure-devopsazure-pipelinesazure-devops-server-2020

From within a Build/Release pipeline, can we discover its path?


In Azure DevOps, we can organize our Build/Release definitions into high-level folders:

enter image description here

Example: for every pipeline that resides in the Framework folder, I want to conditionally execute a certain task. The pre-defined Build and Release variables provide a plethora of ways to discover information about the underlying file system, but seemingly nothing for this internal path information.

During a pipeline run, is it possible to determine the folder/path that it resides in?


Solution

  • You can check it with Rest API - Builds - Get:

    GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=6.0
    

    In the response you get the definition details including the path:

    "definition":  {
                       "drafts":  [
    
                                  ],
                       "id":  13,
                       "name":  "TestBuild",
                       "url":  "https://dev.azure.com/xxxxx/7fcdafd5-b891-4fe5-b2fe-xxxxxxx/_apis/build/Definitions/13?revision=1075",
                       "uri":  "vstfs:///Build/Definition/13",
                       "path":  "\\Test Folder",
                       "type":  "build",
                       "queueStatus":  "enabled",
                       "revision":  1075,
                       "project":  {
                                       "id":  "7fcdafd5-b891-4fe5-b2fe-9b9axxxxx",
                                       "name":  "Sample",
                                       "url":  "https://dev.azure.com/xxxx/_apis/projects/7fcdafd5-b891-4fe5-b2fe-9xxxxxx",
                                       "state":  "wellFormed",
                                       "revision":  97,
                                       "visibility":  "private",
                                       "lastUpdateTime":  "2021-03-22T10:25:39.33Z"
                                   }
                   },
    

    So:

    Write-Host "##vso[task.setvariable variable=isFramework;]true"
    

    Now, in the task add a custom condition:

    and(succeeded(), eq(variables['isFramework'], 'true'))