This is an improved github action to handle replacing environment secrets in our .env files. It validates the .env file to ensure that we actually have defined a secret for each key in the .env file where it is required.
- The action will look for a file named
.env.${environment-name}where${environment-name}is the value of theenvironment-nameinput. - Your .env file should use the convention
SECRET_KEY={SECRET_KEY}for any key you want replaced - The action will check your secrets you pass in, and replace them using the following priority:
- ENVIRONMENT_NAME_SECRET_KEY (e.g. STAGING_SECRET_KEY will replace SECRET_KEY in .env.staging)
- SECRET_KEY (e.g. SECRET_KEY will replace SECRET_KEY in .env.staging only if STAGING_SECRET_KEY is not defined)
The following is an example of how to use this action in your github workflow.
name: Replace Environment Secrets
uses: bythepixel/env-replacer-action@1.0.0
with:
environment-name: staging
env-file-path: .env
secrets: ${{ toJSON(secrets) }}If you have additional variables you would like to include that are not secrets but are dynamic, you can pass them in as well using the additional-variables input.
name: Replace Environment Secrets
- name: Replace Environment Secrets
uses: bythepixel/env-replacer-action@1.0.0
with:
environment-name: staging
env-file-path: .env
secrets: ${{ toJSON(secrets) }}
additional-variables: '{"APP_SHA": "${{ env.sha }}" }'There is an example workflow here that you can run manually in your browser to see the expected output. You can cross reference the examples directory as well as the defined secrets for this repository to understand how the action works.
- This action is written as a "composite" action, meaning it runs on github runner that uses it.
- It does not use docker or any other dependencies. It is written in Ruby with no gem dependencies. Github runners come with Ruby pre-installed and we are not using any version specific features.
- The moment you need to use a gem, you will need to update the action to install a specific ruby version and bundle install the gems.
- This action will take the input file, replace all the keys with the secrets you pass in, and write to the file you specify. It will delete the original "environment specific" version of the file.
- Ruby (asdf is the recommended version manager)
- Clone the repository
- Run
bundle installto install the required gems
rakebundle exec standardrb --fix