azure-pipelines-agent: [YAML] Cannot set environment variable in reusable step

In YAML you can set environment variables using scripts easily but if you try to do the same thing in a reusable step then the changes are ignored in the original YAML script. So you cannot create reusable steps that will do any sort of calculations that must be used in later (reusable) steps.

A typical YAML scenario.

- script: echo ##vso[task.setvariable variable=SomeVariable]true

Trying to clean up the build by using reusable steps.

- template: steps/variable-set.yml@templates
  parameters:
     name: SomeVariable
     value: SomeValue


# variable-set.yaml
parameters:
  name: ''
  value: ''
  
steps:
- script: echo ##vso[task.setvariable variable=${{parameters.name}}]${{parameters.value}}

Note that this is just an example to test whether it can be done. The actual reusable step would be doing common calculations that all builds would need.

Have you tried trouble shooting?

Tried all variants I could think of. I tried marking the variables as output and that didn’t change anything. I tried declaring the variables in the root YAML file and then setting their value later. The original value was used. I tried using both echo and powershell to set the values. Neither worked.

Agent Version and Platform

Version of your agent? 2.102.0/2.100.1/…

OS of the machine running the agent? OSX/Windows/Linux/…

VSTS Type and Version

VisualStudio.com or On-Prem TFS? VSTS

If On-Prem TFS, which release? 2015 RTM/QU1/QU2/…

If VisualStudio.com, what is your account name? http://fsmb.visualstudio.com

What’s not working?

No errors. The variables just aren’t set. If they already exist then the values aren’t changed. If they don’t already exist then the variables aren’t created.

Fundamentally what I need to do is be able to get data from a reusable step back to the root YAML file without having to go through a hacky solution like saving a file temporarily or something.

Agent and Worker’s Diagnostic Logs

Logs are located in the agent’s _diag folder. The agent logs are prefixed with Agent_ and the worker logs are prefixed with Worker_. All sensitive information should already be masked out, please double check before pasting here.

Related Repositories

Please ensure you are logging issues to the correct repository in order to get the best support.

  • Tasks Repository - contains all of the inbox tasks we ship with VSTS/TFS. If you are having issues with tasks in Build/Release jobs (e.g. unreasonable task failure) please log an issue here.
  • Hosted Agent Image Repository - contains the VM image used in the VSTS Hosted Agent Pool. If you are having Build/Release failures that seems like they are related to software installed on the Hosted Agent (e.g. the DotnetSDK is missing or the AzureSDK is not on the latest version) please log an issue here.

If you are hitting a generic issue about VSTS/TFS, please report it to the Developer Community

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

You can try using the format function (poorly) described here: https://github.com/Microsoft/vsts-agent/blob/master/docs/preview/yamlgettingstarted-templateexpressions.md In your example it would be: - script: ${{ format('echo ##vso[task.setvariable variable={0}]{1}', parameters.name, parameters.value) }}