coryvb123
coryvb123

Reputation: 412

VSTS Build - Conditional variable based on SourceBranchName

I would like to set the Build Number differently based on which branch initiated the build.

For example:

Upvotes: 1

Views: 1448

Answers (1)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

There isn't any way to do it directly since you need to transform the branch name to number.

Two workarounds I can think:

  1. Create three build definitions for the three branches so that you can configure different build number format for them.
  2. With only one build definition, add a Power-Shell script task in your build definition to get the source branch name and then update the build number base on the it.

The PowerShell script:

$branch = $Env:Build_SourceBranchName
Write-Host "Current branch is $branch"
if ($branch -eq "Dev")
{
    $NewBuildNumber = "3" + ".X.Y.Z"
    Write-Host "Update Build Number To: $NewBuildNumber"
    Write-Host "##vso[build.updatebuildnumber]$NewBuildNumber"
}
elseif ($branch -eq "Beta")
{
    $NewBuildNumber = "2" + ".X.Y.Z"
    Write-Host "Update Build Number To: $NewBuildNumber"
    Write-Host "##vso[build.updatebuildnumber]$NewBuildNumber"
}
elseif ($branch -eq "Live")
{
    $NewBuildNumber = "1" + ".X.Y.Z"
    Write-Host "Update Build Number To: $NewBuildNumber"
    Write-Host "##vso[build.updatebuildnumber]$NewBuildNumber"
}

Upvotes: 7

Related Questions