TFS 2015 发布管理访问构建变量
TFS 2015 Release management access build variables
在 TFS 2015 中,我们有一个构建,它将自动触发一个新版本。
它是用新的 script based build definitions.
实现的
现在我想将用户变量从构建传递到发布。
我在构建中创建了一个变量 "Branch"。
在自动触发的版本中我尝试访问它。但它总是 empty/not 设置。
我用 $(Branch)
和 $(Build.Branch)
试过了。
我还尝试使用这些名称在发行版中创建一个变量,但没有成功。
是否有机会从发布的构建定义中访问用户变量?
我现在用一些自定义的 powershell 脚本来做。
在构建任务中,我编写了一个 XML 文件,其中包含我在发布任务中需要的变量。 XML 文件稍后是工件的一部分。
因此,首先我使用 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中的节点名称].
读取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"
在 TFS 2015 中,我们有一个构建,它将自动触发一个新版本。 它是用新的 script based build definitions.
实现的现在我想将用户变量从构建传递到发布。 我在构建中创建了一个变量 "Branch"。
在自动触发的版本中我尝试访问它。但它总是 empty/not 设置。
我用 $(Branch)
和 $(Build.Branch)
试过了。
我还尝试使用这些名称在发行版中创建一个变量,但没有成功。
是否有机会从发布的构建定义中访问用户变量?
我现在用一些自定义的 powershell 脚本来做。
在构建任务中,我编写了一个 XML 文件,其中包含我在发布任务中需要的变量。 XML 文件稍后是工件的一部分。
因此,首先我使用 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中的节点名称].
读取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"