22

I am trying to update an enviroment variable in TeamCity using Powershell script. However, it does not update the value of the variable. How can I do this?

Below is my current code which gets the currentBuildNumber fine:

$currentBuildNumber = "%env.currentBuildNumber%"
$newBuildNumber = ""
Write-Output $currentBuildNumber
If ($currentBuildNumber.StartsWith("%MajorVersion%") -eq "True")
{
 $parts = $currentBuildNumber.Split(".")
 $parts[2] = ([int]::Parse($parts[2]) + 1) + ""
 $newBuildNumber = $parts -join "."
}
Else
{
 $newBuildNumber = '%MajorVersion%.1'
}

//What I have tried
$env:currentBuildNumber = $newBuildNumber
Write-Host "##teamcity[env.currentBuildNumber '$newBuildNumber']"
Write-Host "##teamcity[setParameter name='currentBuildNumber' value='$newBuildNumber']"
ErikE
  • 48,881
  • 23
  • 151
  • 196
Jake Rote
  • 2,177
  • 3
  • 16
  • 41
  • For my own reference, see https://confluence.jetbrains.com/display/TCD9/Build+Script+Interaction+with+TeamCity for more info about the ## notation – David Gardiner May 02 '16 at 07:35

1 Answers1

27

Try

"##teamcity[setParameter name='env.currentBuildNumber' value='$newBuildNumber']"

(note the env. prefix in the name)

Also, you can try to increase the PowerShell std out column default (80 using TeamCity's command runner). If your service message is longer than that, then TeamCity will fail to parse it.

if ($env:TEAMCITY_VERSION) {
    $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(8192,50)
}
SteveChapman
  • 3,051
  • 1
  • 22
  • 37