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