0

In Azure devops I want to use a variable inside a task but I want the contents of the variable rather than the name itself, so it's an extra level of indirection.

somecommand --value `"$(MyVariable)`"

MyVariable ultimately comes from a Variable group set up in devops and listed in the variables section at the top of the file.

So if I had a variable MyVariable="foo" and another my variable group had a member foo="bar" would expect this to be the outcome.

somecommand --value "bar"

How do I do this extra level of indirection?

Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87
  • Are you using Bash to execute `somecommand`? If so, you can do `somecommand --value "${!MyVariable}"`. – d125q Jan 02 '20 at 13:38
  • I'm just using Azure Devops I don't know what that uses under the hood – Rob Sedgwick Jan 02 '20 at 15:49
  • 1
    For now, the nested variable like `$($(MyVariable))=>$(foo)=>bar` is not supported in azure devops task, an alternative way to do that is set your variable `MyVariable`=`$(foo)` instead of `foo`... We can also use inline PS task to do that like [this](https://stackoverflow.com/a/56215166/10910450) and [this](https://stackoverflow.com/a/37113070/10910450). – LoLance Jan 03 '20 at 02:43

1 Answers1

1

How do I do this extra level of indirection?

If you have foo = bar in Variable Group and MyVariable = foo in pipeline variables, VSTS doesn't support nested variables like $($(MyVariable)) even after we've linked Variable Group.

To make your somecommand --value "$(MyVariable)" expand to somecommand --value "bar", you should set MyVariable = $(foo) instead of foo in pipeline variables.

enter image description here

In this way the $(MyVariable) could be expanded to bar when being passed to your command.

And another alternative way is to use Inline Powershell task to set the variable.You may get some help from these two similar issues(one, two).

LoLance
  • 25,666
  • 1
  • 39
  • 73