-
-
Notifications
You must be signed in to change notification settings - Fork 36
Loading environment variables from .env files
Alex Skrypnyk edited this page Jul 17, 2019
·
1 revision
It is possible to implement simplified version of Dotenv, which allows to read environment variables from .env
file and export them into current execution environment, by altering entrypoint
:
entrypoint:
- bash
- "-c"
- "-e"
- |
[ -f .env ] && [ -s .env ] && export $(grep -v '^#' .env | xargs) && if [ -f .env.local ] && [ -s .env.local ]; then export $(grep -v '^#' .env.local | xargs); fi
bash -e -c "$0" "$@"
- '{{cmd}}'
- '{{name}}'
The script above handles multiple features:
- Runs all commands with
bash
by default. - Fails ahoy command if any of the sub-commands failed (the
-e
flag). - Reads
.env
file:- Checks if file exists.
- Checks if file is not empty.
- Reads non-empty and non-hash-starting lines (comments).
- Exports each variable into running environment context (see below for an important details).
- Reads
.env.local
file (can be used for local overrides):- Same as above for
.env
file. - Overrides variables set in
.env
file.
- Same as above for
- Runs command with passed arguments
Any environment variables set within .ahoy.yml
file, including variable exported from .env
files (like in the script above), are set only for the duration of currently running ahoy command. The variables are not "spilled" into outside environment. Any environment variables set outside of ahoy command are accessible to ahoy subcommands.