Chubsdad
Chubsdad

Reputation: 25537

Current directory in Azure DevOps yml

Is there a portable way of knowing the directory in which the current yml file exists in Azure DevOps. This would something equivalent of %~dp0 in dos batch files.

Thanks,

Upvotes: 2

Views: 8690

Answers (2)

Jaime Alonso
Jaime Alonso

Reputation: 41

I know this is an old question with an answer, but just wanted to add that a really fast way to just get the current directory is using the command:

$(System.DefaultWorkingDirectory)/(Add Desired folder here)/(Add Desired folder or file here)

Upvotes: 4

Leo Liu
Leo Liu

Reputation: 76910

Is there a portable way of knowing the directory in which the current yml file exists in Azure DevOps.

When you open the yml file in the Azure Devops, you will noticed that there is a path containing the file at the top of yml:

enter image description here

If you want to use scripts to get the path, you could use the Definitions - Get:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1

In the return body, there is option yamlFilename with the path for the yml:

"process": {
    "yamlFilename": "/TestASP.NETProject/TestASP.NETProject/test2.yml",
    "type": 2
},

Then we could use powershell or batch to parse this path to get the directory of the current yml file exists.

Update:

I have two follow up qns: a) How can I setup a generic service connection and b) how do I get the build definition id. Is it one of the predefined variables in the pipeline?

First, we could use PAT to setup a generic service connection in the powershell task, like:

- task: PowerShell@2
  inputs:
   targetType : inline
   script: |
     $url = "https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1"
     $connectionToken="Your PAT Here"
     $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

     $buildPipeline= Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get    

     $YamlFilename= $buildPipeline.process.yamlFilename

     Write-Host This is Build Result: $YamlFilename

Second, for the definition id, you could get it from the browser directly when you open/select your pipeline:

enter image description here

Hope this helps.

Upvotes: 2

Related Questions