Change environment variable value between tasks in

2019-06-21 11:10发布

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

4条回答
Fickle 薄情
2楼-- · 2019-06-21 11:38

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

$ENV:SuperVersion = "2.0"
Write-Host ("##vso[task.setvariable variable=SuperVersion;]"3.2"")
#  Output will be "2.0"
Write-Output $ENV:SuperVersion     
$ENV:SuperVersion = "5.5"
#  Output will be "5.5" but only within the scope of this task.
Write-Output $ENV:SuperVersion 

NEXT TASK

Write-Output $ENV:SuperVersion     
# Output is "3.2"
查看更多
Evening l夕情丶
3楼-- · 2019-06-21 11:47

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/

查看更多
够拽才男人
4楼-- · 2019-06-21 12:00

Based on this issue following line will do the trick.

Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")

You may find more commands like that in here

查看更多
等我变得足够好
5楼-- · 2019-06-21 12:00

Environment variables created with $env: are Process 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 or Machine environment variable:

[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'User')

[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'Machine')

I'm not sure though, that it will work in VS Team Services, you'd have to test it.

Reference:

查看更多
登录 后发表回答