nag
nag

Reputation: 930

VSTS Build variables update not working at queue new build

Below is the variable declaration and power shell script I've written to update the build variable at queue new build

Declaration:

enter image description here

Power shell script:

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

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

Output:

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

From above output it's still showing variable value as '1' instead of '11'.

Next task - Update assembly info -> where I'm not getting updated value.

enter image description here

Am I missing anything?? Please help me out.

Upvotes: 3

Views: 1975

Answers (3)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51103

Yes, you are missing something important, actually the value is updated. However, it could only be used as following task, not at the task you updated, that's why the output of your powershell script still showing variable value as '1' instead of '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

For a test, you could add a powershell script after the task which you updated build variables as follow:

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

From the result you could see, the value of ActualRevision has been changed to 11 at following task.

enter image description here


Update:

My script:

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


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

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

Test Script(another task):

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

Upvotes: 2

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

You need to output the task logging command. So the code for powershell script should be:

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

And then you can use it in following tasks.

Upvotes: 1

DenverDev
DenverDev

Reputation: 497

You should be able to just use: $env:ActualRevision = $temp

Powershell Script

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

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

VSTS Output

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\1\s\test.ps1'
Before: 123 - 2
After: 123 - 123

Upvotes: 1

Related Questions