TFS 2015发布管理访问构建变量

Chr*_*ris 8 release-management tfs-2015

在TFS 2015中,我们有一个构建版本,它将自动触发新版本.它是使用基于脚本的构建定义实现的.

现在我想将一个用户变量从build传递给release.我在构建中创建了一个变量"Branch".

在此输入图像描述

在自动触发的版本中,我尝试访问它.但它总是空的/没有设置.

$(Branch)和它一起尝试过$(Build.Branch).我还尝试使用这些名称在发布中创建变量,但没有成功.

是否有机会从发布中的构建定义中访问用户变量?

Chr*_*ris 5

我现在使用一些自定义的powershell脚本来执行此操作。

在构建任务中,我编写了一个XML文件,其中包含发布任务中所需的变量。XML文件是稍后Artifact的一部分。

因此,首先,我使用XML文件的路径,变量名和当前值调用自定义脚本:

在此处输入图片说明

Powershell脚本就是这样。

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)
Run Code Online (Sandbox Code Playgroud)

这将导致这样的XML:

<Variables>
  <Branch>Main</Branch>
</Variables>
Run Code Online (Sandbox Code Playgroud)

然后,将其复制到工件暂存目录,以便它成为工件的一部分。

在发布任务中,我使用另一个powershell脚本,该脚本通过读取xml来设置任务变量。

第一个参数是xml文件的位置,第二个参数是任务变量(必须在发布管理中创建变量),最后一个是xml中的节点名称。

在此处输入图片说明

读取xml并设置变量的功能如下:

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"
Run Code Online (Sandbox Code Playgroud)