MatterOfFact
MatterOfFact

Reputation: 1545

How to set value of variable group variable in TFS Build

In this thread the setting of a variable group variable in an Azure Devops build is discussed. My question is if there is also a way to set the value of a variable group variable by a TFS Build. To be concrete: I'd like to store the current value of the $(Rev:r) variable of a build pipeline in a variable group to use it in a different build pipeline.

Upvotes: 1

Views: 626

Answers (1)

Leo Liu
Leo Liu

Reputation: 76670

My question is if there is also a way to set the value of a variable group variable by a TFS Build

We could still use the Logging Command to set the value of a variable group variable by a TFS Build:

Write-Host "##vso[task.setvariable variable=testvar;]testvalue"

However, if you want store the current value of the $(Rev:r) variable of a build pipeline in a variable group to use it in a different build pipeline, there will be two problems here.

The first one is that the variable $(Rev:r) could not be used in the command line or powershell task, since the colon : is not supported. This variable $(Rev:r) is specifically defined by Azure devops, we cannot call it outside of azure devops. So, we could not use the $(Rev:r) inside the task, like:

Write-Host "##vso[task.setvariable variable=testvar;]$(Rev:r)"

To resolve this issue, we could set this variable $(Rev:r) in the Build number format of Options tab in the build pipeline:

enter image description here

Now, we could use the predefined variables Build.BuildNumber to get the value of $(Rev:r). The variable Build.BuildNumber is supported by command line or powershell task, we could use it:

Write-Host "##vso[task.setvariable variable=testvar;]$(Build.BuildNumber)"

The second question is that the variables set with the Logging Command can only be applied to the current agent environment, and will not be saved to the task group. So it could not be used to different build pipeline. It will still use the value I originally set in the taskgroup, and you could check the value in task group, it is not modified by our Logging Command.

To resolve this issue, we need use REST API to update the real value in the task group from web page:

Please check the UPDATE3 answer in this thread for the detailed steps to update the value in the task group.

So, we need use REST API and Build.BuildNumber to achieve your request.

Upvotes: 1

Related Questions