gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:22:30 24 4
gpt4 key购买 nike

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

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

enter image description here

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

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

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

最佳答案

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

然后我将它复制到 artifact staging 目录中,以便它成为 artifact 的一部分。

在发布任务中,我使用了另一个 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"

关于tfs-2015 - TFS 2015 发布管理访问构建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com