我正在尝试使用TFS RELEASE_ENVIRONMENTID和RELEASE_RELEASEID的发行版变量获取值,以便我可以检查发行版CD中的所有步骤是否成功。如果任何步骤失败,则只需回滚更改:
Param([string] $BackupFolderName,[string] $TargetFolderName,[string] $releaseid,[string] $envid)
$releasequeryuri = "http://example.net:8080/tfs/DefaultCollection/82d987654cfd-01ea-46c0-81d1-b2c98768508663/_apis/Release/releases/$(releaseid)/environments/$(envid)/tasks?api-version=3.0-preview.1"
$releasequeryresult = Invoke-RestMethod -Uri $releasequeryuri -Method Get -ContentType "application/json" -UseDefaultCredentials
foreach ($i in $releasequeryresult.value)
{
if ($i.status -eq "failed")
{
Get-ChildItem -Path $(BackupFolderName) | Copy-Item -Destination $(TargetFolderName) -Force -Recurse
Write-Host "Copy complete"
}
else
{
Write-Host "Nothing to copy"
}
}
,配置变量部分中的变量如下:
但是它不会获取变量的值。在发布管道的初始化作业步骤中,我肯定会得到这些值。我们将不胜感激。
我还尝试将变量值仅设置为RELEASE_RELEASEID和RELEASE_ENVIRONMENTID,但不会获取这些值。
答案 0 :(得分:3)
如果要在PowerShell上使用发布环境变量,则无需创建新变量,只需在脚本中使用TFS变量即可:
$envId= $env:RELEASE_RELEASEID
$releasequeryuri = "http://example.net:8080/tfs/DefaultCollection/82d987654cfd-01ea-46c0-81d1-b2c98768508663/_apis/Release/releases/$(releaseid)/environments/$(envId)/tasks?api-version=3.0-preview.1"