Use multiple .env.{environment} files #1069
kasperpeulen
started this conversation in
General
Replies: 1 comment
-
|
I'm open to this 👍 I'd look at a PR that adds varlock. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
At this moment, the environment variables are spread all over the place.
Development
The .env.example file is checked in.
These files are copied by the init script to .env which is git ignored, and the SESSION_SECRET is replaced with a random string.
For local development, those values are used.
For testing in CI,
.env.exampleis being used.Production
For production, none of these environment variables are used, they are set in the docker file:
Not sure why this INTERNAL_COMMAND_TOKEN need to be stored in the .env file.
And can not be passed as environment variable to the server.
Some are set only in the build part of the docker file:
Staging
To override some of the variables for staging, they can be overriden in the stating deploy step:
Secrets
Some secrets (not all are really secrets though) are added by
fly secrets setmanually (or in the init script).The init script will set ALLOW_INDEXING=false to staging, and won't set this variable for production.
Test
For testing, environment variables are set in
playwright.config.ts:PORT=3000, NODE_ENV="test"The rest of the variables for testing are coming from in
.env.examplein CI, and.envfor local testing.Proposal
Proposal, clean this up, by using multiple
.env.{environment}and.env.{environment}.localfiles.This pattern became popular in Ruby on Rails in the 2010's, as part of the the Twelve-Factor App methodology (https://12factor.net/config). Later it also adopted by the JS ecosystem (CRA, Nextjs and Vite).
The advantage of using multiple files, is that you easily can see which environment variables will be used in each environment.
The epic stack has 4 environments: development (local), test (ci), staging (fly) and production (fly)
The gitignored
.env.{environment}.localfiles allow to slightly modify the environment variables when run locally.And add production secrets that can not be committed.
For example, the epic stack could use the following:
How to implement
The dotenv package can be configured to read the APP_ENV and then run the right set of env files based on the APP_ENV.
Node also has a built in --env-file argument:
https://nodejs.org/api/cli.html
But this will not work for loading env variables in docker and github actions. Or if you want to load those env variable files into any other custom command.
This package is created by the maintainer of dotenv to solve that loading .env files for custom commands:
https://github.com/dotenvx/dotenvx
Another new package, varlock, seems more promising to me though;
https://github.com/dmno-dev/varlock
It uses the
.env.{environment}.localhierachy convention out of the box.Has good conventions for security.
And has intergrations for vite, docker and github actions.
Beta Was this translation helpful? Give feedback.
All reactions