New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always load vars defined in .env files, providing .env.local.php for prod #501

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
3 participants
@nicolas-grekas
Member

nicolas-grekas commented Dec 3, 2018

Q A
License MIT

This PR provides a nice experience where .env files are "always" loaded.
That means their purpose is easy to reason about: the committed files define the defaults (.env especially) - and .env.local defines local overrides.

A new .env.local.php is provided for prod, so that performance is not affected.
When .env.local.php is found, other .env files are ignored.

On the deployement side, this means it should be much easier to deal with .env files for people that can't easilly deal with real env vars:

  • if you use real env vars they always win
  • logically, .env files are always loaded (to define defaults)
  • on prod, one can dump a .env.local.php file for max performance

The plan is to add a new flex command to create this file and ease deployments:
composer dump-env prod

@symfony-flex-server

Pull request passes validation.

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from ba785ca to 41345a6 Dec 3, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 41345a6 to 6701cad Dec 10, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas

This comment has been minimized.

Member

nicolas-grekas commented Dec 10, 2018

@symfony/deciders votes welcome.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 6701cad to c350161 Dec 10, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from c350161 to b2bf7e6 Dec 10, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from b2bf7e6 to cd9d848 Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas changed the title from Always load vars defined in .env files, using .env.local.php for prod cache to Always load vars defined in .env files, using .env.cache.php for prod on the web Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas

This comment has been minimized.

Member

nicolas-grekas commented Dec 11, 2018

(updated to use .env.cache.php instead of .env.local.php)

@lyrixx

This comment has been minimized.

Member

lyrixx commented Dec 11, 2018

Things are getting really complicated IMHO. We have so many .env files.
It was much simpler when we were using .yaml file.
I totally understand why we needed to switch to .env file, but IMHO we should rethink everything.

For example why do we need a committed .env file while we say that we need to defined a default value for each env var in the services.yaml ? (or another file). We need a default value for the default value ?
(Actually, this is a rhetorical question. I know the reason: because if we want to use env var (not resolved I mean) even in the DIC we need this .env).

But how many people really need really dynamic values ? very few.
We are making things really complicated because of a few people, leaving so many others behind
There are plenty of questions like "how does env var work", "what should I put in my env var", "how do I change my DB in test" etc etc. Theses question are on the table almost everyday on slack (different slack / different channel).
Most of the people I work with / meet / train or discuss with do not understand where we are going and think it's now more complicated than before.

Symfony 4 + Flex + MakerBundle is really cool and easy. But theses env var really needed to be re-thought

@nicolas-grekas

This comment has been minimized.

Member

nicolas-grekas commented Dec 11, 2018

@lyrixx 💯 - that's what this PR provides :)

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from cd9d848 to 7873fe0 Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 7873fe0 to bccee62 Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@lyrixx

This comment has been minimized.

Member

lyrixx commented Dec 11, 2018

BTW, this PR is a very good move 👍

About this point (the part "which you should"):

if you use real env vars they always win (which you should - see https://12factor.net/)

I totally disagree. It's like always : it depend. Many applications are hosted on bare metal, and will not move. Using env var make things more complicated because you need to load them for FPM + CLI + CRON + Worker. Using a simple .env file (like you porpose) is much more simple.

So we should not say :

you should use env var.

Declaring parameter in .env for just fine and easy for many!

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from bccee62 to 262d20b Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas changed the title from Always load vars defined in .env files, using .env.cache.php for prod on the web to Always load vars defined in .env files, providing .env.local.php for prod Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 262d20b to 8036c6c Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 8036c6c to 0a7ed06 Dec 11, 2018

@symfony-flex-server

Pull request passes validation.

@symfony-flex-server

Pull request passes validation.

@nicolas-grekas

This comment has been minimized.

Member

nicolas-grekas commented Dec 11, 2018

After a pretty long discussion with @weaverryan on Slack, we figured out a very simple way to make all this work:

  • logically load .env files all the time
  • provide a .env.local.php file for prod - when found it replaces all .env files
  • add a new dump-env command to flex so that deploying would be just composer dump-env prod (PR to come)

@nicolas-grekas nicolas-grekas force-pushed the nicolas-grekas:env-always-load branch from 0a7ed06 to 0648faf Dec 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment