How do I dynamically get the environment variables value based on My branch variable name?


#1

I have a bash script as below

- run: 
          command: |
            echo ${CIRCLE_BRANCH}_USERNAME
            echo ${"${CIRCLE_BRANCH}_USERNAME"}

What I am trying to do is I have an Environment variable created in the circle project settings .Lets say its name is master_USERNAME .

How do i get its value without having to hardcode it and use dynamic naming convention instead using branch name ?

Lets say I have environment variables for each branches created in the circle CI environment variables , I want to have one .YML file and it should read environment variables dynamically ? Is it possible


#2

From your example, you would do:

    - run: eval "echo $"${CIRCLE_BRANCH}"_USERNAME"

Though in this example, you’d need to be careful with eval and make sure you’re only running what you think you’re running.


#3

Thanks that works great


#4

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.