0

i am trying since two days to figure out how to archive this but i don't find the right way.

I just want to get the value of an pipeline variable. The thing is i have to concatenate two strings, which i get as parameters during the pipeline run and call the matching pipeline variable. My current approach is to combine the parameters in a bash task and call the pipeline variable with the bash variable as identifier but this does not work. Either the prefix variable is empty or the pipeline does not start because of an error. Or sometimes the result is "line 2: dev_handler1: command not found".

This is my structure:

variables-template.yaml

variables:
  prefix: ""
  dev_handler1:"prefix1"
  dev_handler2:"prefix2"
  dev_handler3:"prefix3"
  dev_handler4:"prefix4"
  <.....>

step-template.yaml

parameters:
  - name: environment
    type: string
  - name: handlers
    type: object
    default:
      - handler1
      - handler2
      - handler3
      - handler4
steps:
   - ${{ each handler in parameters.handlers }}:
    - bash: |
        prefixVariable="${{ parameters.environment }}_${{ handler }}"
        prefix=$(${prefixVariable})
        echo "##vso[task.setvariable variable=prefix;]${prefix}"

    - bash: |
       < do other stuff where i need the prefix variable >

pipeline.yaml

parameters:
 - name: environment
   displayName: Environment
   type: string


variables:
  - template: variables-template.yaml

stages:
 - stage:
   jobs:
    - job:
      steps:
       - template: step-template.yaml
         parameters:
           environment: ${{ parameters.environment }}

I tried calling the variable in the following ways:


environment2=${{ parameters.environment }}
handler2=${{ handler }}

prefix=$(${environment2}_${{ handler }})
prefix=$("${environment2}_${{ handler }}")
prefix="${{ variables['${environment2}_${{ handler }}'] }}"
prefix=${{ variables.${environment2}_${{ handler }} }}
prefix=$('${{ parameters.environment }}_${{ handler }}')

# with extra variable
prefixVariable="${environment2}_${{ handler }}" # returns e.g. dev_handler1

prefix=$(${prefixVariable})
prefix=$[variables.${prefixVariable}]
prefix=${{ variables['"${prefixVariable}"'] }}

# set prefixVariable in extra bash task
- bash: |
   prefixVariable="${{ parameters.environment }}_${{ handler }}" # returns e.g. dev_handler1
   echo "##vso[task.setvariable variable=prefixVariable;]${prefixVariable}"

- bash: |
   prefix=${{ variables.$(prefixVariable) }}
   prefix=${{ variables['$(prefixVariable)'] }}
   prefix=$($(prefixVariable))

# with format function
prefix=$($[format('${0}_${1}', ${{ parameters.environment }}, ${{ handler }})])
prefix=$($[format('${0}_${1}', ${environment2}, ${{ handler }})])
prefix=$($[format('${0}_${1}', ${environment2}, ${handler2})])
prefix=${{ variables[$[format('${0}_${1}', ${{ parameters.environment }}, ${{ handler }})]] }}
prefix=${{ variables[$[format('${0}_${1}', ${environment2}, ${{ handler }})]] }}
prefix=${{ variables[$[format('${0}_${1}', ${environment2}, ${handler2})]] }}

# with concat function - got error each time
prefix=$(concat('"${environment2}"', '_', '${{ handler }}'))
prefix=$(concat('${{ parameters.environment }}', '_', '${{ handler }}'))
prefix=$(@concat('${{ parameters.environment }}', '_', '${{ handler }}'))
prefix=$('@concat('${{ parameters.environment }}', '_', '${{ handler }}')')

My feeling is that i looked up every related question on StackOverflow but here are some questions i looked into: Combine variables in Azure Devops Build Pipeline YAML Azure DevOps pipeline template - how to concatenate a parameter Azure pipeline concatenating variable names and accessing new variable value Azure DevOps: Getting variable value by concatenating other variables'value as task input Azure pipelines bash tries to execute the variable instead of expanding

Edit

I work in an environment where its not that easy to use tasks from the marketplace so i would prefer a solution where it is not necessary to use custom tasks.

1 Answer 1

0

Based on your YAML sample, you need to use nested variable: $(${{ parameters.environment }}_${{ handler }}) to get the Pipeline variable value.

To achieve your requirement, you can use the Variable Set task from extension: Variable Toolbox.

Here is the example:

Variables.yaml

variables:
  prefix: ""
  dev_handler1: prefix1
  dev_handler2: prefix2
  dev_handler3: prefix3
  dev_handler4: prefix4

step-template.yaml

parameters:
  - name: environment
    type: string
  - name: handlers
    type: object
    default:
      - handler1
      - handler2
      - handler3
      - handler4
steps:
- ${{ each handler in parameters.handlers }}:
  - task: VariableSetTask@3
    inputs:
      variableName: 'prefix'
      From: 'value'
      value: '$(${{ parameters.environment }}_${{ handler }})'

Main YAML:

parameters:
 - name: environment
   displayName: Environment
   type: string

variables:
   - template: variables-template.yaml

stages:
 - stage:
   jobs:
    - job:
      steps:
       - template: step-template.yaml
         parameters:
           environment: ${{ parameters.environment }}

It will create a new variable based on the nested variable:$(${{ parameters.environment }}_${{ handler }}). Then you can use the $(prefix) in the next tasks.

Update:

To use the bash script, you can use the following sample:

step-template.yaml

parameters:
  - name: environment
    type: string
  - name: handlers
    type: object
    default:
      - handler1
      - handler2
      - handler3
      - handler4
steps:
- ${{ each handler in parameters.handlers }}:
  - bash: |
     prefix='$(${{ parameters.environment }}_${{ handler }})'
     echo "##vso[task.setvariable variable=prefix;]${prefix}"

  - bash: |
       echo $(prefix)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply and I'm pretty sure this would work but I'm in an corporate environment and not allowed to install tasks that easily.... is there any way to accomplish this without the task?
@Frayzerck Yes. Please check the update in the answer. You can use the format: $(${{ parameters.environment }}_${{ handler }}) in Bash Script and it can also work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.