Write-Host "##vso[task.setvariable 未设置变量值

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

我使用“经典”方法而不是 YAML 创建了一个发布管道。

变量 ReleasePipelineRunTime 已声明为“已在管道中更新”值。

我添加了一个 v2 powershell 任务来运行以下行脚本

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

未设置变量,记录的输出为

2023-10-24T08:05:00.2706627Z ##[section]Starting: Set variables
2023-10-24T08:05:00.3055374Z ==============================================================================
2023-10-24T08:05:00.3056063Z Task         : PowerShell
2023-10-24T08:05:00.3056356Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2023-10-24T08:05:00.3056581Z Version      : 2.180.1
2023-10-24T08:05:00.3056844Z Author       : Microsoft Corporation
2023-10-24T08:05:00.3057137Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2023-10-24T08:05:00.3057368Z ==============================================================================
2023-10-24T08:05:02.2667659Z Generating script.
2023-10-24T08:05:02.2676359Z ========================== Starting Command Output ===========================
2023-10-24T08:05:02.2692790Z ##[command]"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\ADOAgents\Prod\_work\_temp\1c1aa42b-ddf5-4e0b-955d-b4101a84fa2d.ps1'"
2023-10-24T08:05:02.2696432Z now: 202310240905
2023-10-24T08:05:02.2714701Z ReleasePipelineRunTime: Updated in pipeline
2023-10-24T08:05:02.2855986Z ##[section]Finishing: Set variables

首先,为什么ReleasePipelineRunTime的值仍然是“Updated in pipeline”而不是“202310240905”?

其次,我查看了已定义变量的列表,但似乎没有一个存储发布管道运行时间的变量,这看起来很奇怪。 O还有我没发现的吗?

提前致谢

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

task.setvariable
不会在同一步骤中更新,因为它更新了环境变量。您必须在下一步中读取变量。

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

Step2:
  "ReleasePipelineRunTime: $(ReleasePipelineRunTime)"
© www.soinside.com 2019 - 2024. All rights reserved.