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.