filip
filip

Reputation: 1503

VSO Build vNext - get TFS revision number

In my build steps I need to provide TFS revision number as a paramter. I know there is a variable $(Build.SourceVersion) but it returns it with a "CS" prefix e.g. "CS1234"

Is there an easy way to remove that "CS" prefix? Any built-in string functions?

Thanks

Upvotes: 2

Views: 2150

Answers (3)

antmeehan
antmeehan

Reputation: 933

Adding to @cilerler's answer, I've used this generic Powershell script as my first VSTS build step:

Param(
    [string] $SourceBranchName = $Env:BUILD_SOURCEBRANCHNAME,
    [string] $SourceVersion = $Env:BUILD_SOURCEVERSION
)

Write-Host "SourceBranchName = $SourceBranchName"
Write-Host "SourceVersion = $SourceVersion"

if ($SourceBranchName -eq "trunk"){
    $MajorMinor = "0.0"
}
else{
    $MajorMinor = $SourceBranchName
}

# Remove CS from changeset number
$Revision = ($SourceVersion -replace 'C','')

$BuildNumber = "$MajorMinor.$Revision.0"

Write-Host "Setting Build Number '$BuildNumber'"

Write-Host "##vso[build.updatebuildnumber]$BuildNumber"

Note that it presumes that your branch name is in the format Major.Minor (eg. 1.4) or trunk.

Also, it seems the VSTS documentation is wrong, the Build.SourceVersion variables is of the form C1226.

Upvotes: 0

cilerler
cilerler

Reputation: 9420

There is no build in method. Also changeset number and revision number are not the same thing. You will get COMMIT ID in $(Build.SourceVersion) if you are using GIT repository.

You may use PowerShell to strip that part off. Following line should give you the numeric portion.

[string]$version = ($Env:BUILD_SOURCEVERSION -replace'\D+(\d+)','$1')

Upvotes: 1

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32240

I'm not aware about any built-in functions to format that string - you'll have to do it in your script somewhere. Note that $(Build.SourceVersion) variable returns empty string if you specify it in the Build Number Format field in your build definition. This thread might give you more details.

Upvotes: 1

Related Questions