'How to Define Github Branch Specific Secret or an Alternative?

For example, travis-ci.com allows the user to define an env variable which is bound to a certain branch. Whereas Github does allow you to define secret env variables, they, however, all have to compete for the same namespace which introduces additional complexity. Does anyone know a workaround?



Solution 1:[1]

I have not seen that particular feature, which means you need to define your variable environment with a naming convention:

branchName_myVariable

In your GitHub Action script, you can then reference said variable by prepending the branch name you would have extracted in a separate step.

Solution 2:[2]

As an alternative, you can create an environment on GitHub. And you can create secrets for that environment. And finally, you can add a deployment branch for that environment. That means you could have a job to deploy to that environment using that secret. Only that job would have access to the secret and you can also set a rule to only allow deploys to that environment using workflows on that branch.

I think, that way you get what you want (or maybe a little bit more restrictive) because only that branch would have access to the secret but you have to define an environment. I suppose in most cases when you introduce a secret that depends on a branch it's because that branch represents an environment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC
Solution 2 josecelano