VSTS 构建变量更新在队列新构建中不起作用

VSTS Build variables update not working at queue new build

下面是我编写的变量声明和电源 shell 脚本,用于更新队列新构建中的构建变量

声明:

电源shell脚本:

$fileData = Get-Content -Path C:\builds\agent\PreviousRevisionUpdate.txt
$temp=$fileData[1]

##vso[task.setvariable variable=ActualRevision;]$temp
Write-Host "$temp - $env:ActualRevision"

输出:

2018-02-06T15:29:19.6035251Z ##[section]Starting: Actual Build Number Update
2018-02-06T15:29:19.6035251Z ==============================================================================
2018-02-06T15:29:19.6035251Z Task         : PowerShell
2018-02-06T15:29:19.6035251Z Description  : Run a PowerShell script
2018-02-06T15:29:19.6035251Z Version      : 1.2.3
2018-02-06T15:29:19.6035251Z Author       : Microsoft Corporation
2018-02-06T15:29:19.6035251Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2018-02-06T15:29:19.6035251Z ==============================================================================
2018-02-06T15:29:19.6095263Z ##[command]. 'C:\builds\agent\_work\_temp\dd262af4-0863-4f8d-a14e-1d9ea50b4c72.ps1' 
2018-02-06T15:29:20.1186281Z 11 - 1
2018-02-06T15:29:20.1386321Z ##[section]Finishing: Actual Build Number Update

从上面的输出来看,它仍然将变量值显示为“1”而不是“11”。

下一个任务 - 更新程序集信息 -> 我没有获得更新值的地方。

我错过了什么吗??请帮帮我。

你应该可以只使用:$env:ActualRevision = $temp

Powershell 脚本

$temp=123
Write-Host "Before: $temp - $env:ActualRevision"

$env:ActualRevision = $temp
Write-Host "After: $temp - $env:ActualRevision"

VSTS 输出

Task         : PowerShell
Description  : Run a PowerShell script
Version      : 1.2.3
Author       : Microsoft Corporation
Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
==============================================================================
. 'D:\a\s\test.ps1'
Before: 123 - 2
After: 123 - 123

是的,您遗漏了一些重要的东西,实际上 值已更新。但是,它只能用作以下任务,不能用于您更新的任务,这就是为什么您的 powershell 脚本的输出仍然将变量值显示为“1”而不是“11”。

Sets a variable in the variable service of taskcontext. The first task can set a variable, and following tasks are able to use the variable. The variable is exposed to the following tasks as an environment variable.

Source Link: Logging Commands

对于测试,您可以在更新构建变量的任务之后添加一个 powershell 脚本,如下所示:

Write-Host "After: - $env:ActualRevision"

从结果可以看出,ActualRevision 的值已在以下任务中更改为 11。


更新:

我的脚本:

$temp=11
Write-Host "Before: $temp - $env:ActualRevision"


Write-Host ##vso[task.setvariable variable=ActualRevision;]$temp

Write-Host "After: $temp - $env:ActualRevision"

测试脚本(另一个任务):

Write-Host "After: - $env:ActualRevision"

您需要输出任务日志命令。所以powershell脚本的代码应该是:

Write-Host "##vso[task.setvariable variable=ActualRevision;]$temp"

然后您可以在以下任务中使用它。