28

I'm currently using Build in Visual Studio Team Services (was Visual Studio Online), and would like to be able to set a Build Variable in a Build Step so that the new value can be used in a subsequent Build Step.

Obviously you can set it before the Build starts but I'm looking to late bind the variable during a subsequent Build Step.

Is this possible?enter image description here

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Simian
  • 801
  • 1
  • 12
  • 20
  • 1
    Have you tried the approach [described here](https://github.com/Microsoft/vso-agent-tasks/issues/375)? See the answer from @ChrisPatterson – Yan Sklyarenko Oct 01 '15 at 14:44

4 Answers4

31

When inside of a script you can update a variable by emitting the following in your ps1

"##vso[task.setvariable variable=testvar;]testvalue"

You can then pass the variable into the next script using $(testvar)

JoeBrockhaus
  • 2,745
  • 2
  • 40
  • 64
  • 5
    This works unless you're using Task Groups. I created an issue which contains a workaround: https://github.com/Microsoft/vsts-tasks/issues/3116 – George Tsiokos Nov 18 '16 at 20:35
7

This doc from the API talks about what ##vso commands you can use.

Don't forget to set system.debug to true. It seems there is a bug that muted stdout and thus, all ##vso are not working.

https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md

Compulim
  • 1,148
  • 10
  • 18
  • 1
    System.debug is set in a VSTS variable, see https://learn.microsoft.com/en-us/vsts/build-release/actions/debug-deployment-issues – raterus Jan 03 '18 at 17:08
1

You can create a powershell script an reference it as a build task. Then inside your powershell scripts add this:

"##vso[task.setvariable variable=key]value"

After that on all your tasks you can read the variable as $(key). If you want to protect your variable, use:

"##vso[task.setvariable variable=secretVar;issecret=true]value"

And then use it as $(secretVar) in your next tasks.

Rodrigo Werlang
  • 2,118
  • 1
  • 17
  • 28
1

I found this link helpful: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell

This has the complete options of what you can do: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch

You can reuse set variable from task to task, and also job to job. I couldn't find anything on stage to stage.

In summary:

jobs:

# Set an output variable from job A
- job: A
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
    name: setvarStep
  - script: echo $(setvarStep.myOutputVar)
    name: echovar

# Map the variable into job B
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar
Ryan Harlich
  • 157
  • 1
  • 7