Abdul Majeed
Abdul Majeed

Reputation: 35

Unable to get the value of variable inside a variable in azure pipelines

I'm trying to use variables inside variables in azure pipelines. Below is an example of the bash script:

#!/bin/bash

customer=google
environment=preprod
android_google_preprod_account_activation_url=preprod.google.com

echo "Customer is $customer"
echo "Environment is $environment"

var1=android_${customer}_${environment}_account_activation_url

echo "variable is $var1"
echo "original value is ${!var1}"

I get the expected output for the above bash script when I run it on my Ubuntu server, with NO errors:

Customer is google
Environment is preprod
variable is android_google_preprod_account_activation_url
original value is preprod.google.com

The yml code for azure pipelines is:

parameters:
  - name: customer
    displayName: 'select customer'
    type: string
    values:
      - google
  - name: environment
    displayName: 'select environment'
    type: string
    values:
      - preprod

variables:
  - group: android-${{ parameters.customer }}-${{ parameters.environment }}
  - name: var1
    value: android-${{ parameters.customer }}-${{ parameters.environment }}-account-activation-url

script: |
      echo "Customer is $(customer)"
      echo "Environment is $(environment)"
      echo "variable is $(var1)"
      echo "original value is $(!var1)"
    displayName: 'echo variables'

The value of android-google-preprod-account-activation-url is being taken from variable groups inside library.

It gives me an error for the 4th line:

invalid indirect expansion

The first 3 lines output is as expected.

Expected output is:

Customer is google
Environment is preprod
variable is android_google_preprod_account_activation_url
original value is preprod.google.com

Is there a different syntax that needs to be followed in azure pipelines?

Upvotes: 0

Views: 2049

Answers (2)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13469

The macro syntax "$(varName)" is a proprietary syntax in Azure Pipelines to interpolate variable values. It is processed during runtime and different with the syntax "${varName}" in Bash scripts.

For your case, you can try to use the compile time syntax "${{ variables.varName }}" to get the value in the pipeline.

echo "original value is $(${{ variables.var1 }})"

With above change, after you triggered the pipeline:

  • At the compile time, the expression "${{ variables.var1 }}" will be replaced with the actual value "android_google_preprod_account_activation_url". So, the expression "$(${{ variables.var1 }})" will be changed to "$(android_google_preprod_account_activation_url)".
  • Then at the runtime, the expression will be parsed as the correct value "preprod.google.com".

Below is an example I have tested on my side.

YAML
variables:
  android_google_preprod_account_activation_url: 'preprod.google.com'
  var1: 'android_google_preprod_account_activation_url'

jobs:
- job: A
  displayName: 'Job A'
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: none
  - task: Bash@3
    displayName: 'Print variables'
    inputs:
      targetType: inline
      script: |
        echo "android_google_preprod_account_activation_url = $(android_google_preprod_account_activation_url)"
        echo "var1 = $(var1)"
        echo "original value = $(${{ variables.var1 }})"
Result

enter image description here

For more details, you can reference the related document "Understand variable syntax".

Upvotes: 2

Shamrai Aleksander
Shamrai Aleksander

Reputation: 15998

I`m not a bash expert ))) however... you're trying to use the parameters expansion What is indirect expansion? What does ${!var*} mean?

But it refers to the bash variables.... when you define variables in the devops pipeline, you have to use them as environment variables or through the macro.

or something like that:

android_google_preprod_account_activation_url=preprod.google.com

echo "Customer is $(customer)"
echo "Environment is $(environment)"

var1=android_$(customer)_$(environment)_account_activation_url

echo "variable is $var1"
echo "original value is ${!var1}"

Upvotes: 1

Related Questions