Zysce
Zysce

Reputation: 1240

Unable to change variable with powershell in tfs vnext

Technical infos: TFS 2017 (15.112.26307.0) with VS 2017

I have a variable called TestVar in my process with default value set at "Test".

In my build process, I have a powershell task calling a script. I want to change the value of TestVar, so in my script, I have those lines:

echo $Env:TestVar
$newValue = "NewValue"
Write-Host ("##vso[task.setvariable variable=TestVar;]$newValue")
echo $Env:TestVar

However, in my build output, the value is not changed:

Test
Test

I looked for any issue with vsts-tasks commands, but I found nothing

Is there any problem in the syntax?

EDIT: after testing, the variable is modified if I print it in another task.

However, I want to set a custom source label.

enter image description here

The source label is set by variable ProductBuildVersionNum.

This variable is modified by a script on build process.

At the end of the build, the label set is the initial value of the variable.

Is there any way to set the source label programmatically or to force the build process to use the new variable value?

Upvotes: 0

Views: 539

Answers (1)

Marina Liu
Marina Liu

Reputation: 38106

When you changed a variable, it will effect for the following steps. So you just need to use another PowerShell task after this one (as you found), and you will find the variable TestVar’s value changed to NewValue.

Using the variable as Label format in Label sources step, it seems the updated variable’s value can’t be detected. And I create an issue Why does label source step not use the modified variable's value which defined in label format, you can follow up.

For now, the work around is update the build number with the variable $(ProductBuildVersionNum) if you don’t care about the name of completed build $(Build.BuildNumber) to be changed. Steps as below:

  1. In Repository Tab, change the Label format as $(Build.BuildNumber).
  2. Add a PowerShell task after you current PowerShell task (modify variable value), with the logging command:

    Write-Host ("##vso[build.updatebuildnumber]$(ProductBuildVersionNum)").

Upvotes: 1

Related Questions