Devops 发布管道:使用 Write-Host 在部署组任务中设置的变量值的范围是什么“##vso[task.setvariable variable=

问题描述 投票:0回答:1

我可以找到有关使用

Write-Host "##vso[task.setvariable variable
设置的变量值范围的文档似乎围绕构建管道。

发布管道的范围是什么?

我有一个包含 3 个部署组的发布管道。

第一个设置变量如下。

$now=$(Get-Date -Format "yyyyMMddHHmm");
"now: $now"
Write-Host "##vso[task.setvariable variable=ReleasePipelineRunTime]$now"

这似乎根据需要设置了变量。我可以通过后续的 powershell 任务来证明这一点,该任务只是将变量的值输出到管道日志。

我想在以下两个部署组任务中使用,但是在这些部署组中,变量重置回默认值。

新值是否应在后续部署组中的所有任务中可用?

如果不是,如何设置才能如此?

我注意到 task.setvariable 有一个“isoutput”选项,但相关文档指出该变量需要使用前缀来使用,但任务似乎没有 id,所以我不确定它是否与这里相关。

提前致谢。

azure-devops azure-pipelines-release-pipeline
1个回答
0
投票

部署组任务中设置的变量值的范围是什么 使用写入主机“##vso[task.setvariable 变量=ReleasePipelineRunTime]$now”

变量在工作范围内。对于经典的构建管道,文档中注明了设置多作业输出变量

经典发布管道也是如此。

解决方法: 您可以在第一个作业中将变量值保存到其他位置,然后在后续作业中获取它。

例如,创建一个变量组并使用 REST API 更新并获取变量值。

在工作 1 中:

Powershell 任务 1 现在获取时间:

$now=$(Get-Date -Format "yyyyMMddHHmm");
"now: $now"
Write-Host "##vso[task.setvariable variable=ReleasePipelineRunTime]$now"

Powershell 任务 2 设置 ReleasePipelineRunTime REST API:

$token = ""

$url="https://dev.azure.com/{ORG}/{PROJECT}/_apis/distributedtask/variablegroups/{VariableGroupID}?api-version=5.0-preview.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  
  "variables": {
    "ReleasePipelineRunTime": {
      "value": "$(ReleasePipelineRunTime)"
    }

  },
  "type": "Vsts",
  "name": "variable group1",
  "description": "Updated variable group"

}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json

Powershell任务3检查ReleasePipelineRunTime:

Write-Host $(ReleasePipelineRunTime)

工作2:

Powershell 任务 1 获取 ReleasePipelineRunTime REST API:

$token = ""

$url="https://dev.azure.com/{ORG}/{PROJECT}/_apis/distributedtask/variablegroups/{VariableGroupID}?api-version=6.0-preview.2"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
ForEach ($variable in $response.variables)
{

  $name="ReleasePipelineRunTime"
  $variablevalue =  $variable.$name.value
  echo  $variablevalue

}
Write-Host "##vso[task.setvariable variable=ReleasePipelineRunTime]$variablevalue"

Powershell任务2检查ReleasePipelineRunTime:

Write-Host $(ReleasePipelineRunTime)
© www.soinside.com 2019 - 2024. All rights reserved.