TFS 2015 发布管理访问构建变量 [英] TFS 2015 Release management access build variables

查看:25
本文介绍了TFS 2015 发布管理访问构建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TFS 2015 中,我们有一个构建版本,它将自动触发新版本.它是通过新的

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

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

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

解决方案

我现在使用一些自定义的 powershell 脚本.

在构建任务中,我使用发布任务中所需的变量编写了一个 XML 文件.该 XML 文件稍后将成为 Artifact 的一部分.

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

powershell 脚本是这样的.

参数([参数(强制=$true)][字符串]$xml文件,[参数(强制=$true)][字符串]$变量名,[参数(强制=$true)][字符串]$variableValue)$directory = Split-Path $xmlFile -Parent如果 (!(Test-Path $xmlFile)){如果 (!(Test-Path $directory)){New-Item -ItemType 目录 -Path $directory}外文件 -FilePath $xmlFileSet-Content -Value ""-路径 $xmlFile}$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);$xml["变量"].AppendChild($xml.CreateElement($variableName)).AppendChild($xml.CreateTextNode($variableValue));$xml.Save($xmlFile)

这将产生这样的 XML:

<变量><分支>主</分支></变量>

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

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

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

读取xml并设置变量的powershell是这样的:

参数([参数(强制=$true)][字符串]$xml文件,[参数(强制=$true)][字符串]$taskVariableName,[参数(强制=$true)][字符串]$xml变量名)$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);$value = $xml["变量"][$xmlVariableName].InnerText写主机##vso[task.setvariable variable=$taskVariableName;]$value"

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?

解决方案

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"

这篇关于TFS 2015 发布管理访问构建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆