skip to Main Content

I’m building a release pipeline in Azure Devops. In one of the steps I’m using an inline bash script to retrieve the value of a secret previously retrieved by a KeyVault step.

This is the script:

#!/bin/bash

# Get the comma-separated list of connected systems from the pipeline variable
connected_systems=$(echo "$(connectedSystems)")
echo "${connected_systems}"

# Loop through each connected system
IFS=',' read -r -a connected_systems_array <<< "$connected_systems"
for connected_system_name in "${connected_systems_array[@]}"; do
    
# Get the password value from the corresponding pipeline variable
    password="${!connected_system_name}"
    echo "connected_system_name: ${connected_system_name}"
    echo "password: ${password}"

    # Replace placeholder with password in the file
    sed -i "s/.password=placeholder/.password=$password/g" $(propertiesFile)
done

echo "$(propertiesFile)"
cat $(propertiesFile)

My problem is that I can’t find the way to actually retrieve the value from the pipeline variable with the name of ${connectedSystem}.

I’ve tried:

password=$(${connected_system_name}) -> Doesn't work
password=$(eval echo "${connected_system_name}") -> Doesn't work
password=$(eval "${connected_system_name}") -> Doesn't work

I’m running out of ideas as I thoght I would just need to pass the string value to the $() function to retrieve the value of the pipeline variable…

Any ideas?

Thanks in advance!

2

Answers


  1. My problem is that I can’t find the way to actually retrieve the value from the pipeline variable with the name of ${connectedSystem}.

    In order to retrieve values from the Azure Devops release pipeline variables you need to use the syntax here- $(variablename)

    I have updated your code to get the value of connectedSystem like and its system’s password like below:-

    Bash script:-

    #!/bin/bash
    
    # Get the comma-separated list of connected systems from the pipeline variable
    connected_systems=$(connectedSystems)
    
    echo $(connectedSystems)
    
    # Get the password values directly from the pipeline variables
    system1=$(system1)
    system2=$(system2)
    system3=$(system3)
    
    # Loop through each system and print its value
    for connected_system_name in system1 system2 system3; do
        # Get the password value for the current system
        password=$(eval echo "$$connected_system_name")
        
        # Print system name and password
        echo "connected_system_name: $connected_system_name"
        echo "password: $password"
    done
    
    

    My Release pipeline variables:-

    enter image description here

    Output:-

    enter image description here

    Reference:-
    Define variables – Azure Pipelines | Microsoft Learn

    Complete bash script:-

    #!/bin/bash
    
    # Get the comma-separated list of connected systems from the pipeline variable
    connected_systems=$(connectedSystems)
    echo "connectedSystems: $connected_systems"
    
    # Get the password values directly from the pipeline variables
    system1=$(system1)
    system2=$(system2)
    system3=$(system3)
    
    # Loop through each system and print its value
    for connected_system_name in system1 system2 system3; do
        # Get the password value for the current system
        password=$(eval echo "$$connected_system_name")
        
        # Print system name and password
        echo "connected_system_name: $connected_system_name"
        echo "password: $password"
    done
    
    # Assuming propertiesFile contains the path to file.properties
    propertiesFile="$(System.ArtifactsDirectory)/file.properties"
    
    # Assuming you want to extract the value associated with a key named "example_key"
    example_value=$(grep '^example_key=' "$propertiesFile" | cut -d '=' -f 2)
    
    echo "Value associated with example_key: $example_value"
    
    Login or Signup to reply.
  2. If the ‘connectedSystems‘ is a secret in Azure key vault, the Azure Key Vault task will download the secret value as string and set it as a secret variable in the pipeline. The pipeline secret variable has the same (connectedSystems) with the Azure key vault secret. The values of secret variables will be always masked as "***" in the console logs for security in pipelines. See "Set secret variables".

    After the Azure Key Vault task, on the subsequent steps within the same job, you normally can directly reference the secret variables using the expression "$(varName)".

    However, on the script tasks (such as Bash, PowerShell, CmdLine, etc..), it is recommended to map the secret variables into environment variables and reference the environment variables in the scripts. For example.

    - task: Bash@3
      env:
        MY_SECRET_TOKEN: $(mySecretToken)
      inputs:
        targetType: inline
        script: echo $MY_SECRET_TOKEN | az devops login --org https://dev.azure.com/myorg
    

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