2

I have my release pipeline Variables tab set like:

Environment variables

I would like to access my123 variable in task's display name by concatenating initialVariable's result.

Outputs

I have tried so far referencing only initialVariable and it returned proper value in Job's display name.

enter image description here

But when I try to create my123 value by using initialVariable(=123), I am not getting proper value (was hoping that $(initialVariable) would convert to 123 and $(my123) would get proper "finalValue").

enter image description here

luka032
  • 925
  • 4
  • 12
  • 34
  • This is not how variables work out-of-the-box. You might be able to hack it together with a different powershell task. – Josh Gust Aug 13 '19 at 18:47

2 Answers2

1

It's ugly, but...

Like I mentioned in my comment, I don't think you're going to get this to work in the UI by default.

Luckily you can use PowerShell to hack this together if your REALLY need the ability to address a variable name based on the value of another variable.

All the variables (secrets are handled a little differently) in your build or release pipeline definition are made available to your powershell script FILE (not inline) via environment variables (ie. $env:initialVariable).

Suppose your situation is thus:

selector = selectable1   //this is the value that can change
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection

In this case (assuming I understand your request) you want to be able to change the value of the selector and force tasks to access the appropriate selectable variable.

So...

Define a new variable in your pipeline.

selector = selectable1   //this is the value that can change
selected = ""  //this is the variable you use in your tasks
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection

Write a VariableSelection.ps1 script. This powershell script will be what you need to run to assign the value of $(selected) before it gets used.

# VariableSelection.ps1

Write-Host "select variable: $env:selector"
$selectedValue = (gci env:"$env:selector").value
Write-Host "##vso[task.setvariable variable=selected]$selectedValue"

Note: it is my observation that if you write this script inline, it will not work b/c the environment variable functionality is different for scripts run from a file.

Given the value of $(selector) is selectable2, when the script is run, then the value of the $(selected) will be theSecondSelection.

Example in a Pipeline

Powershell

enter image description here

YAML

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
  name: Hosted VS2017

variables:
- name: "selector"
  value: "var1"
- name: "selected"
  value: ""
- name: "var1"
  value: "var1_value"
- name: "var2"
  value: "var2_value"

steps:
- task: PowerShell@2
  inputs:
    filePath: '$(build.sourcesdirectory)/varSelector.ps1'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "env:selected: $env:selected"
      Write-Host "selected: $(selected)"

Results

enter image description here

Josh Gust
  • 4,102
  • 25
  • 41
  • I have defined following Variables: **selectable1 (firstselection) - selectable2 (secondselection) - varSelected ("") - varSelector (selectable1)**, but and first task PowerShell task: **Write-Host "##vso[task.setvariable variable=varSelected]$((gci env:$env:varSelector).value)"** and it's not updating varSelected variable at all, even when printing insame task, even when printing as display name to task 2. – ldragicevic Aug 13 '19 at 23:50
  • I think my answer may have made some assumptions about how your pipeline is setup. I'll edit with some screen shots of my working example. – Josh Gust Aug 14 '19 at 16:35
  • Maybe I could help you by giving wider context about what I would like to achieve. I would like to create parallelized jobs (Multi-Configuration) and based on Multiplier value to define additional variables which will be unique for each job being run. So any approach in which I could get unique variables as task's inputs based on Multiplier is welcome! Thanks for helping. – ldragicevic Aug 14 '19 at 19:02
  • @JoshGust Is there any way to make this work for Key Vault secrets? – Dan Dec 10 '19 at 19:03
  • If you wanted to set a variable value to the value of a Key Vault secret, I don't know if that would work, because AzDO works to keep secret values out of the output stream and the mechanism for writing variable values is with the output stream. – Josh Gust Dec 10 '19 at 23:10
1

Azure DevOps: Getting variable value by concatenating other variables'value as task input

This is a known issue. Because the value of nested variables (like $(my$(initialVariable)) are not yet supported in the build/release pipelines.

Check my other thread for some details.

The workaround is add a Run Inline Powershell task to set the variable based on the input pipeline variables, just like Josh answered.

For you case, I test it by following Powershell scripts:

 if ($(initialVariable)-eq "123")
 {
   Write-Host "##vso[task.setvariable variable=my123]finalvalue"
 }
 else
 {
   Write-Host "##vso[task.setvariable variable=my123]otherValue"
 }

Then we could get the variable my123 based on the value of variable initialVariable in following task, I add command line task to display the value:

enter image description here

In the result, the value in the command line task is correct finalvalue. But the display name is still $(my123):

enter image description here

Important:

That is also the question in your comment. This behavior is expected. That because the variable in the display name is just to get the predefined value. It's static acquisition, not dynamic. The variable my123 is assigned when running powershell. The static variable my123 in the display name does not go in to the environment where the powershell code is running.

So, the variable my123 in the title could not get the value in the task powershell. But other task could use it very well.

Hope this answer clean your puzzle.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • This option **hard codes** the value getting assigned to the `my123` variable into the powershell script. – Josh Gust Aug 14 '19 at 18:52