RandomUs1r
RandomUs1r

Reputation: 4188

How to access build folder number in TFS 2017

I typically tell TFS what folder to use via $(build.sourcesdirectory) which translates into C:\Builds\1\s .

However, I need to repack the deployment zip, so I had to add Content\C_C\Builds\myproj\1\s to my CI.

This isn't very good as the 1 can change due to dev ops work.

Is there a variable or technique I can use to replace the 1 with a variable?

I tried $(Agent.Id) with no luck...

Upvotes: 1

Views: 262

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30432

You can use PowerShell to retrieve the number from variable $(build.sourcesdirectory) and pass the value to a new variable.

Dynamically get the number Sample:

$string = "$env:BUILD_SOURCESDIRECTORY"
#$string = "E:\andyli-v\01Agent\_work\6\s" (The length is 29, the number '6' in the string is the 27th character, So get the substring as (26,1) means get one character behind the 26th character)
$parameter = $string.Substring(26, 1)
Write-Host "BUILD_SOURCESDIRECTORY is :" $string
Write-Host "The String length is : "$string.Length
Write-Host "The number is : "$parameter

You can use $parameter directly to get the number if do actions within the same script.

Set a variable :

If you want a variable, you can set it via PS :

Write-Output ("##vso[task.setvariable variable=GetNumber;]$parameter")

You can reference there articles to set the variable:

enter image description here

Upvotes: 2

Related Questions