Search code examples
githubyamlgithub-actions

How to parameterize secrets on GitHub Action


We have two branches in repository (dev/prd), each representing a deployment environment. Also we have GitHub action secrets for each branch, in dev branch it should be dev_react_api, in prd branch it should be prd_react_api.

Now we are working on a GitHub action workflow using these secrets secrets.dev_react_api and secrets.prd_react_api

Is there a solution to parameterize GitHub action secrets like the following ?

# only pseudo-code
env:
  branch_name: github.ref

secrets["${env.branch_name}_react_api"]

Solution

  • It should work exactly like you have shown with the dynamic name. secrets is just a variable and you provide the key name either explicitly secrets.x implicitly secrets['x']. Building your key dynamicly works just fine as such. The additional env branch_name is also unneeded since you can just get that value directly in the string.

    If you have a paid GitHub plan or are using a public repo, you can also take a look at Environments which take care of this entirely by instead defining two separate environments with the required secrets each.