Double substitution on Github Action - Nested substitution

I have a problem with a double substitution on a GitHub Action. I have a secret named: MASTER_ENVIRONMENT_NAME and STAGING_ENVIRONMENT_NAME

On GitHub actions, I am trying to use the value of the env variable depending on the branch like this:

BRANCH_NAME=${GITHUB_REF##*/}
DEPLOYMENT=secrets.${BRANCH_NAME^^}_ENVIRONMENT_NAME
echo ${{ secrets.${BRANCH_NAME^^}_ENVIRONMENT_NAME }} // Failing here

I have tried multiple combinations but all failing, any thought?

Maybe like this:

- name: Determine branch name
  id: branch-name
  run: |
    BRANCH_NAME="${GITHUB_REF##*/}"
    echo "::set-output name=upper::${BRANCH_NAME^^}"
- name: Pick secret dynamically
  run: |
    echo "secrets.${{ steps.branch-name.outputs.upper }}_ENVIRONMENT_NAME:"
    echo "${{ secrets[format('{0}_ENVIRONMENT_NAME', steps.branch-name.outputs.upper)] }}"

Hi @Simran-B thank you for your reply.

Actually, I found the solution not that long ago doing the following:

- name: Set Branch Credentials
  run: |
    BRANCH_NAME=${GITHUB_REF##*/}
    BRANCH_NAME=${BRANCH_NAME^^}
    echo "BRANCH_ENVIRONMENT_NAME=${BRANCH_NAME}_ENVIRONMENT_NAME" >> $GITHUB_ENV
    
- name: Deploy 
  run: echo ${{ secrets[env.BRANCH_ENVIRONMENT_NAME] }}

Thank you very much!

Cool, I think yours is easier to parse.

BTW. Is there a way to combine ${NAME##...} and ${NAME^^} in bash?

//edit: Bash doesn’t seem to support nesting

I am not sure if it is possible to combine ${NAME##...} and ${NAME^^}

However, it is possible to do this

TEST="part-1/part-2/part-needed"
echo ${TEST##*/} | tr [a-z] [A-Z] // PART-NEEDED

Thank you :slight_smile:

1 Like