In a project I’ve been working on we faced a problem. While using Github Actions to handle our CI/CD, we wanted to reuse our deployment scripts across all
environments for each app. Thus giving us less code to maintain.
The plan was to set up identical secret keys (with different values though) in two different Github repo environments. The actions would trigger on
changes to either the dev branch or the main branch. The source code from each branch would then be deployed to either the dev or production environment,
using the dev or prod secrets accordingly.
But how do we make the action know when to get which secrets and where?
The following solution did the trick, but it’s a bit of a backyard hack. To follow along, you should have some basic knowledge of YAML and Github Actions.
Step 1
For this example I’ve added two environments in the repo from which I will build my app.
They both contain a secret named MY_PRETTY_SECRET. Although they have the same name/key, they contain different values. This could for instance be an API key,
a password, a URL or such. Depending on if I want to deploy this application to my dev environment or my production environment, I want to retrieve the
corresponding value for that environment using only one script. Instead of having two identical scripts, each specific for each environment.
Add a YAML file under the .github/workflows folder in your project
In the example above I’ve separated the logic into multiple jobs. You can of course do this in a single job, eliminating the need for exposing the current_env value.