Search code examples
powershelltfsrelease-management

Set and use enviroment variables in multiple release tasks in TFS


I have a web application that I build and release using TFS. The release definition contains a task group with all the necesarry steps for the deployment of the web application.

What I want to do is to determine a certain value and store it in a variable to use later on in the release proccess in the other release tasks.

At this moment, the first step in de task group is a Powershell task that determines the necessary value and stores it in an environment variable using:

Write-Output ("##vso[task.setvariable variable=MyVar;]$var")

When I use this environment variable in the next Task (Again, a PowerShell task), it runs perfectly as expected using:

Write-Host "Doing stuf for: $env:MyVar"

It's when I want to use the variable as a parameter for (multiple) different task(s) when it gets weird. When the environment variable has no default value, the calculated value from the first PowerShell task is used and all is well. But TFS doesn't like it when environment variables have no default value and forces to provide one before being able to save the release definition again. When I provide a default value, the task that uses the variable as Task parameter uses the default value, instead of the calculated value. I would expect that the calculated value should be used, as the second PowerShell task ensures that the calculated value is properly stored.

So, the symptoms I see:

  • When not providing a default value, the code in scripts as wel as the task parameters use the calculated value
  • When providing a default value, the code in scripts use the calculated value and the task parameters use the default value

Is there something I do wrong, or am I using environment variables wrong and should I use a different method?


Solution

  • As I understand it, you want to define your variable in a powershell-task within the task group, but you are also being forced to provide a value in the environment variable, causing the variable sometimes to have the value set in the powershell, sometimes to have the value defined in the environment variable.

    The solution is to just provide the name of the environment variable itself, so it always gets overwritten by the value you set in the powershell task.

    Source: https://blogs.msdn.microsoft.com/harshillodhi/2016/11/29/vststfs-understanding-task-groups-and-its-various-use-cases-with-setvariable-logging-command/ Under the header "What is a setvariable logging command and how to use it with Task Groups?", three scenario's are referenced. It sounds like the second scenario may fit your needs.