问题
In TFS 2015 we have a build, that will automatically trigger a new release. It is realized with the new script based build definitions.
Now I want to pass a user variable from build to release. I have created a variable "Branch" in the build.
In the automatically triggered release I try to access it. But it is always empty/not set.
I tried it with $(Branch)
and $(Build.Branch)
.
I also tried to create a variable in release with these names, without success.
Is there any chance, to access a user variable from the build definition in the release?
回答1:
I do it now with some custom powershell scripts.
In the build task I write a XML file with the variables I need in the release task. The XML file is part of the Artifact later.
So first of all I call my custom script with the path to the XML file, the variable name and the current value:
The powershell script is like this.
Param
(
[Parameter(Mandatory=$true)]
[string]$xmlFile,
[Parameter(Mandatory=$true)]
[string]$variableName,
[Parameter(Mandatory=$true)]
[string]$variableValue
)
$directory = Split-Path $xmlFile -Parent
If (!(Test-Path $xmlFile)){
If (!(Test-Path $directory)){
New-Item -ItemType directory -Path $directory
}
Out-File -FilePath $xmlFile
Set-Content -Value "<Variables/>" -Path $xmlFile
}
$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);
$xml["Variables"].AppendChild($xml.CreateElement($variableName)).AppendChild($xml.CreateTextNode($variableValue));
$xml.Save($xmlFile)
This will result in an XML like this:
<Variables>
<Branch>Main</Branch>
</Variables>
Then I copy it to the artifact staging directory, so that it is part of the artifact.
In the release task I use another powershell script, that sets a task variable by reading the xml.
The first parameter is the position of the xml file, the second the task variable (you have to create the variable in the release management) and the last is the node name in the xml.
The powershell to read the xml and set the variable is like this:
Param
(
[Parameter(Mandatory=$true)]
[string]$xmlFile,
[Parameter(Mandatory=$true)]
[string]$taskVariableName,
[Parameter(Mandatory=$true)]
[string]$xmlVariableName
)
$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);
$value = $xml["Variables"][$xmlVariableName].InnerText
Write-Host "##vso[task.setvariable variable=$taskVariableName;]$value"
来源:https://stackoverflow.com/questions/39075188/tfs-2015-release-management-access-build-variables