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

8
在TFS 2015中,我们有一个构建,会自动触发新的发布。它是通过新的基于脚本的构建定义实现的。
现在我想将用户变量从构建传递到发布。我已经在构建中创建了一个名为“Branch”的变量。

enter image description here

在自动触发的发布中,我尝试访问它。但它总是为空/未设置。
我尝试使用$(Branch)$(Build.Branch)。 我还尝试使用这些名称在发布中创建变量,但没有成功。
是否有任何机会可以从构建定义中访问用户变量来进行发布?

请看这里,并查看是否有帮助:https://lajak.wordpress.com/2011/03/13/pass-relative-path-arguments-to-msbuild-in-tfs2010-team-build/ - lokusking
嗨,lokusking,我正在使用新的基于脚本的发布管理。抱歉我没有提到它。 - Chris
1个回答

5
我现在使用一些自定义的PowerShell脚本来完成这个任务。
在构建任务中,我编写了一个包含所需变量的XML文件,该XML文件随后成为Artifact的一部分。
因此,首先我使用XML文件的路径、变量名称和当前值调用我的自定义脚本:

enter image description here

这个 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)

这将导致生成如下的XML:
<Variables>
  <Branch>Main</Branch>
</Variables>

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

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

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

enter image description here

读取 XML 并设置变量的 PowerShell 代码如下:

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"

我不得不做一个小改变。看起来buildbinaries部分没有改变,所以逻辑会追加。我把Out-file和Set-Content移到了那个括号的外面。这样每个文件都被覆盖了,然后我把那个文件复制到了该构建的工件中。好的解决方案! - NINtender

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接