Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update .env file and share among teammates?

I created .env file with params, pushed to github, my teammates downloaded repo. In next push I added .env file to .gitignore. Now I need to make changes to .env file, but how they will get it if .env ignored. What is the right way of doing such of manipulation?

UPDATE: I used two libraries to manage env variables: https://www.npmjs.com/package/dotenv https://www.npmjs.com/package/config

like image 343
Sunny Avatar asked Sep 06 '25 02:09

Sunny


1 Answers

You do not store configured .env file in repository but instead, you create .env.dist (or anything named like that) as a template file, and commit that file. Your .dist file can contain all keys commented out, or all keys and default values. It's all up to you but you need to ensure your template do not contain any sensitive data too:

DB_HOST=
DB_USER=

The main benefit is that you do not have .env in the repo, so each developer can easily setup own system as he likes/needs (i.e. local db, etc) and there's no risk of having such file accidentally overwritten on next pull, which would be frustrating.

Also (again), you do not store any sensitive data in the repository, so while your .env.dist can (and maybe even should) be pre-configured to your defaults, you must ensure any credentials are left empty, so no-one can i.e. run the code against production machine (or figure out anything sensitive based on that file).

Depending on development environment you use, you can automate creation of .env file, using provided .env.dist as template (which useful i.e. with CI/CD servers). As dotenv file is pretty simple, processing it is easy. I wrote such helper tool for PHP myself, but it is pretty simple code and easily can be ported to any other language if needed. See process-dotenv on GitHub for reference.

Finally, if for any reason config setup is complicated in your project, you may want to create i.e. additional small script that can collect all data and write always up to date config file (or upgrade existing etc).

like image 50
Marcin Orlowski Avatar answered Sep 11 '25 07:09

Marcin Orlowski