This Github Action generates a local .env file in a github runner, by extracting environment variables from your secrets or custom defined object, that match all secrets, or any prefix
- First, the action parses the secrets object, and filters out any secrets that do not match the APP prefix.
- After, the action creates a
.env
file (or aprefix.env
if aENV_FILE_NAME
was specified) in the current working directory of the runner. - Then, the action moves the generated file to the custom destination path provided by the user, OR recursively searches up from the current working directory, to find the NEAREST
package.json
file, and moves the.env
file to it's same directory. - Once the action confirms the move, the runner exits, and if no error is present, continues to the next step
Below is an example of the minimum appropriate configuration
name: Create Env File
uses: aasmal97/[email protected]
with:
APP_SECRETS: ${{toJson(secrets)}}
APP_SECRETS
: Takes in a stringified JSON object that holds all your secrets or variables (required)ENV_FILE_NAME
: If you want to customized the .env name (i.elocal.env
, etc), add the desired name here. (optional)WORKING_DIRECTORY_PATH
: The ABSOLUTE PATH, that you want the action to start at. If relative paths exist within this value, they are resolved. For examplehome/add/../app
resolves tohome/app
. By default this iscwd
where the action is run (optional)DESTINATION_PATH
: The ABSOLUTE PATH, that you want the .env file to be generated in. If relative paths exist within this value they are resolved. For examplehome/add/../app
resolves tohome/app
. By default, this is the directory where the nearestpackage.json
is found, from theWORKING_DIRECTORY_PATH
value, or if this file does not exist, thecwd
itself. (optional)PREFIX_FILTER
: A regex pattern that matches a secret's name, so it can be extracted. Commonly used to match for prefix patterns likeREACT_APP
. If not defined, it matches ALL secrets passed into the action (optional)
name: Create Env
uses: aasmal97/[email protected]
with:
APP_SECRETS: ${{toJson(secrets)}}
ENV_FILE_NAME: "local"
DESTINATION_PATH: ${{ github.workspace }}/src
- Due to multiple projects having different configs and project directory structures, it is best to provide a
DESTINATION_PATH
value, so the location of the file being generated does not change and is always known. - If you wish to rely on the auto-detection of the nearest
package.json
, and not specify an absolute destination path, try to ensure consistency by setting aWORKING_DIRECTORY_PATH
of the action as close as possible, to the package.json file. Below is an example of this:
name: Create Env
uses: aasmal97/[email protected]
with:
WORKING_DIRECTORY_PATH: ${{ github.workspace }}/src
APP_SECRETS: ${{toJson(secrets)}}