Is there a way to persist changes in environment value between tasks in Visual Studio Team Services? I'm using Powershell to change it but it only changes it in the task not the whole process.
script 1
Write-Verbose "Before: $Env:SuperVersion"
$Env:SuperVersion = $NewVersion
Write-Verbose "After: $Env:SuperVersion"
script 2
Write-Verbose "Final: $Env:SuperVersion"
I see the change at After but Final is always getting the original value
I find that after using Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")
that within the same task, the value has not changed, but in later tasks that value has changed.
This is on TFS 2018 using inline powershell.
FIRST TASK
NEXT TASK
Correct answer has already been posted for this question below, however I think that the discussion presented at the following blog specifically targets the two different ways of setting build variables: one in which the variable will be available only within the specific task in which it is set and another using which you can set a build variable in one task and then access it in another:
https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/
Based on this issue following line will do the trick.
Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")
Environment variables created with
$env
: areProcess
variables, so they're lost when the process exits and you can't access them from another process (PowerShell instance).You need to create
User
orMachine
environment variable:I'm not sure though, that it will work in VS Team Services, you'd have to test it.
Reference: