Home > Software engineering >  Github Actions: Is it Possible to Reference a Variable within a Context?
Github Actions: Is it Possible to Reference a Variable within a Context?

Time:11-11

I would like to be able to reference a github secret based on a project directories name variable so that I only need 1 workflow to handle many projects.

  - name: Build Subject Key Secret String
    run: |
     secret_prefix='WASH_'
     secret_unique=$(echo $ACTOR_NAME | tr 'a-z' 'A-Z')
     secret_suffix='_KEY'
     secret=${secret_prefix}${secret_unique}${secret_suffix}
     echo "SECRET_STRING=$secret" >> $GITHUB_ENV
  - name: Pull Subject Key Secret
    run: |
     echo "WASH_SUBJECT_SECRET=${{ secrets.env.SECRET_STRING }}" >> $GITHUB_ENV 

I Have tried many things to reference the variable within the context including: secrets.SECRET_STRING, secrets.$SECRET_STRING, secrets.${SECRET_STRING}, env.SECRET_STRING (secrets. was concatenated to front of prefix). I also tried to declare env secret in a subsequent step:

        env:
          WASH_SUBJECT_KEY: ${{ secrets.SECRET_STRING }}

I am not sure if it is possible to do something like this, or if there is a better way. If anyone has done something like this before, I would be grateful for some assistance. Thank you!

CodePudding user response:

you could dynamically access to the secret name using square braces, like:

secrets[env. SECRET_STRING]

So you could do something like:

  - name: Pull Subject Key Secret
    run: |
     echo "WASH_SUBJECT_SECRET=${{ secrets[env.SECRET_STRING] }}" >> $GITHUB_ENV 
  • Related