Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting variable reference in github action workflows

Here is my requirement. I have separate GitHub secrets created for each environment.

For eg in GitHub secrets:

STAGING_USERNAME -> <SOME_VALUE>
PROD_USERNAME -> <SOME_VALUE>

I want to construct a new variable and set it in GitHub env like below

name: Build and Deploy to CAA Backend
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  
  workflow_dispatch:
    inputs:
      infraEnv:
        description: 'Env (staging/prod)'     
        required: true
        default: 'staging'
  jobs:
    setup-build-publish:
      name: Test, Build, Deploy, Notify
      runs-on: ubuntu-latest
      steps:
        name: 
          run: |
            echo "USERNAME=${{secrets.${{github.event.inputs.infraEnv}}_username}}" >> $GITHUB_ENV

But this is throwing a syntax error. How to fix this?

like image 454
Kaushik Avatar asked Mar 25 '26 11:03

Kaushik


1 Answers

You can do this with the format function:

${{ secrets[format('{0}_username', github.event.inputs.infraEnv)] }}
like image 62
mhsmith Avatar answered Mar 28 '26 01:03

mhsmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!