Passing release variables between two agent phases

放肆的年华 提交于 2019-12-18 05:54:23

问题


I'm working with TFS on-premise. My issue is that during a release I have two agent phases separated by a manual intervention.

In the first agent phase, I set a variable with:

Write-Verbose $("##vso[task.setvariable variable={0};]{1}" -f $variablename, $variable)

Problem is that in the second agent phase, this variable doesn't exist anymore, even if the same agent is used for the second release phase.

How may I pass a variable between two agent phases during the same release?


回答1:


There is no way to persist variables (no matter powershell variables or VSTS user defined variables) between two agent phases (and environments) for now.

And there is a related issue Variables set via logging commands are not persistent between agents, you can follow up.

The work around for now is define the variable again in next agent phase.




回答2:


You can share a variable between the agent phases by using the TFS Rest API and creating a new variable in the release.

You can create a helper module to facilitate this task.

  1. Get the release by using the environment variable $Env:Release_ReleaseId.
  2. Add a NoteProperty, using Add-Member, to the variables hashtable of the release returned in step 1, where the name is your desired variable name and the value is a ConfigurationVariableValue.
  3. Save the release with the added variable.

In order to use this approach, you would set the variable in your first agent. Then, in the second agent, you can simply read the TFS variable using the $(VariableName) semantic.




回答3:


I've used the "Variable dehydration task" to write the value to my artifact folder in a build pipeline. I then read the json with inline-powershell. Currently, I'm reading it on every task in my release pipeline, which seems mental to me, but it sort of works. You ought to be able to set a global or env? variable and use that instead. Supposedly fixed in 2017, but I'm using 2015.




回答4:


The right way to do it is using Variablegroups to persist between pipelines: https://docs.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml



来源:https://stackoverflow.com/questions/47891756/passing-release-variables-between-two-agent-phases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!