Search code examples
github-actions

Join 2 strings to get the GitHub action secrets


I have few repository secrets in GitHub action, like AWS_ACCESS_KEY_ID_DEV, AWS_ACCESS_KEY_ID_UAT, AWS_ACCESS_KEY_ID_STAGE.

I can take the environment values (DEV, UAT and STAGE) from the branch name. But I want to join these 2 string to get the secrets.

I tried with

env:
 BRANCH_NAME: ${{ github.head_ref || github.ref_name }} 
...
jobs:
  deploy:
   ...
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets['AWS_ACCESS_KEY_ID_'+ env.BRANCH_NAME ] }}
        aws-secret-access-key: ${{ secrets['AWS_SECRET_ACCESS_KEY_'+ env.BRANCH_NAME] }}

But it is not working.


Solution

  • I found a way from this GitHub Doc

    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets[format('{0}_{1}', 'AWS_ACCESS_KEY_ID', env.BRANCH_NAME)] }}
        aws-secret-access-key: ${{ secrets[format('{0}_{1}', 'AWS_SECRET_ACCESS_KEY', env.BRANCH_NAME)] }}