3

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.

nag
  • 920
  • 6
  • 27
  • 51

3 Answers3

2

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"
PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Actually I'm using it in next task only i.e. update assembly info task item as '$(ActualRevision)' – nag Feb 07 '18 at 09:25
  • @nag It's used as an environment variable, have you tried some other format such as `$env:ActualRevision`? And are you using some 3-rd party extension, not sure how they will use and expand the variable? What's the result if you use your own powershell script to set the assembly info? A example: [Version your assemblies](https://learn.microsoft.com/en-us/vsts/build-release/actions/scripts/powershell#example-version-your-assemblies) for your reference. – PatrickLu-MSFT Feb 07 '18 at 09:50
  • Yes I tried $env:ActualRevision and it's giving error like 'Not an interger value' and if i use $(ActualRevision) giving result as '1'. And I'm using VSTS 'Update assembly info' inbuilt task. Can you please check my edited post that I've added snapshot of my next task after power shell script – nag Feb 07 '18 at 09:55
  • @nag :) There is no such a build-in task in my VSTS task, are you using this one? https://marketplace.visualstudio.com/items?itemName=bool.update-assembly-info He said should `Must be a numeric value.` I will suggest you directly use powershell script to do this. For environment variable's concept in build variable please refer https://learn.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch#environment-variables. Your usage seems more like past a variable to variable(revision of the task) in this case may not work. – PatrickLu-MSFT Feb 07 '18 at 10:06
  • Yes. I'm using same task what you provided in the link. Actually based on changeset change only i can increment the revision number in my version so that's why I've added this task and using powershell I'm updating my revision number in temp file and updating env variable at queue time to update my assembly. – nag Feb 07 '18 at 10:28
  • Let me try your example on power shell to update assembly info. Will let you know the results :) – nag Feb 08 '18 at 06:51
  • @nag Haven't noticed this, your original code `##vso[task.setvariable variable=ActualRevision;]$temp` is different from my script, you are missing a write-host, just as Eddie said below. Have you tried to add them? – PatrickLu-MSFT Feb 08 '18 at 08:59
  • my ##vso code is to update the variable on the fly in current queued build, but I'm not getting updated value and that is the issue here. And write host is used to print the data right? – nag Feb 08 '18 at 09:47
  • @nag Actually no, to update the value you need to entire`Write-Host ##vso[task.setvariable variable=ActualRevision;]$temp` code, I just have a test myside, only with `##vso[task.setvariable variable=ActualRevision;]$temp`, the value is not update still with 1. Take a look at the detail tutorial in this link: https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md – PatrickLu-MSFT Feb 08 '18 at 09:57
  • As per your inputs, I've added Write-Host "##vso..." line after my actual ##vso[... but still I'm not getting the updated value. Also tried adding only Write-Host "##vso[..]" and some other changes but no use :( – nag Feb 14 '18 at 11:13
  • @nag Could not reproduce your issue. Have you double checked your format?Such as miss a space. I have update my script for your reference. You could also add a powershell task under your original task for a test instead of using the 3-party task. – PatrickLu-MSFT Feb 14 '18 at 11:47
  • @nag Any update on this, did my reply help to figure out the issue. – PatrickLu-MSFT Feb 25 '18 at 16:46
  • I'm in vacation :) and yes it's working absolutely fine :) Thanks for your time. – nag Mar 02 '18 at 06:07
1

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
DenverDev
  • 467
  • 3
  • 6
  • Variable is updated, but when I use this variable in next step 'Update assembly info' task still I'm getting value as '1'. I've updated my query above please check. – nag Feb 07 '18 at 09:32
1

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.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60