【问题标题】:TFS 2015 Release management access build variablesTFS 2015 发布管理访问构建变量
【发布时间】:2016-12-28 17:57:47
【问题描述】:

在 TFS 2015 中,我们有一个构建,它将自动触发一个新版本。 它是通过新的script based build definitions 实现的。

现在我想将用户变量从构建传递到发布。 我在构建中创建了一个变量“分支”。

在自动触发的版本中,我尝试访问它。但它始终为空/未设置。

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

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

【问题讨论】:

  • 看看here 看看是否有帮助
  • 嗨 lokusking,我正在使用新的基于脚本的发布管理。对不起,我没有提到它。

标签: tfs-2015 release-management


【解决方案1】:

我现在用一些自定义的 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)

这将产生一个像这样的 XML:

<Variables>
  <Branch>Main</Branch>
</Variables>

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

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

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

powershell读取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"

【讨论】:

  • 我必须做一个小改动。看起来 buildbinaries 部分没有改变,所以逻辑会附加。我将 Out-file 和 Set-Content 移到了该括号的外部。这覆盖了每个文件,然后我将该文件复制到该构建的工件中。不错的解决方案!
猜你喜欢
  • 2016-03-06
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2016-10-21
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多