skip to Main Content

I have defined a variable called tst-my–variable, and another one uat-my–variable in my azure pipeline like below.
enter image description here

When I’m running the azure-pipelines.yml, I’m checking the environment in which the pipeline is executing and defining a variable "environmentName".

After that I want to get one of those "tst-my–variable" "uat-my–variable" by concatenating the variable "environmentName" value with the text "-my–variable".
but nothing is working.

steps:
- script: | 
    echo $MY_ENV
  env:
    MY_ENV: $($(environmentName)-my--variable)

I already checked this article but didn’t help.

2

Answers


  1. This is not a direct answer to your question, but allow me to suggest a different approach to solve your problem.

    1. Create variables template for each environment

    As per Variables reuse, create a separate template to store each environment’s variables:

    TEST variables:

    # /pipelines/variables/tst-variables.yaml
    
    variables:
      - name: MY_ENV
        # use another variable's value or, as an alternative, an hard-coded value
        value: $(tst-my--variable)
    
      # Other TEST-related variables here
    

    UAT variables:

    # /pipelines/variables/uat-variables.yaml
    
    variables:
      - name: MY_ENV
        # use another variable's value or, as an alternative, an hard-coded value
        value: $(uat-my--variable)
    
      # Other UAT-related variables here
    

    2. Reference the variables template based on the environment

    The below example pipeline uses a pipeline parameter for the environment, as opposed to a pipeline variable.

    The parameter is then used to dynamically reference the right variables template:

    parameters:
      - name: environmentName
        displayName: 'Environment Name'
        type: string
        default: 'tst'
        values:
          - 'tst'
          - 'uat'
    
    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      - template: /pipelines/variables/${{ parameters.environmentName }}-variables.yaml
    
    steps:
      - checkout: none
    
      - script: | 
          echo "$(MY_ENV)"
        displayName: 'Print the value of pipeline variable'
    
    Login or Signup to reply.
  2. Assuming the value for your variable $(environmentName) is defined during pipeline runtime rather than at compile time, which may prevent the pipeline from including the variable template in template expression syntax, it is suggested you directly use output variables during runtime. Here is a sample for your refernce.

    variables:
      test-my--variable: test123
      uat-my--variable: uat456
    
    steps:
    - bash: |
        if [ "$(environmentName)" = "test" ]; then
          echo "##vso[task.setvariable variable=MY_ENV;isoutput=true]$(test-my--variable)"
        elif [ "$(environmentName)" = "uat" ]; then
          echo "##vso[task.setvariable variable=MY_ENV;isoutput=true]$(uat-my--variable)"
        fi
      name: OutputVar
    - script: |
        echo "$(OutputVar.MY_ENV)"
    

    Image

    See more samples to Set an output variable for use in future jobs and Set an output variable for use in future stages.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search