Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS 2015 Release management access build variables

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.

enter image description here

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?

like image 864
Chris Avatar asked Aug 22 '16 08:08

Chris


People also ask

How do you pass a variable from pipeline to release pipeline?

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.

How do you use a variable group in a 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.

Where is $( build SourcesDirectory?

$(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.


1 Answers

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:

enter image description here

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.

enter image description here

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"
like image 183
Chris Avatar answered Sep 22 '22 08:09

Chris