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?
In order to pass variables between your build and release pipelines you can create/export a file containing your variable on your build agent. This file should be exported as build artifact and then downloaded on the release pipeline.
To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.
$(Build. SourcesDirectory) : The local path on the agent where your source code files are downloaded. For example: c:\agent_work\1\s By default, new build definitions update only the changed files. You can modify how files are downloaded on the Repository tab.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With