BugHunterUK BugHunterUK - 3 months ago 9
Git Question

Pushing repo with environment variables

In my repo on my machine I have a file called

.env
. This contains environment variables for testing as I'm working on the project.

But, when I push to Github I do not want this file included. So I added it to
.gitignore
. I have another file called
.env.sample
that contains empty variables for the user to set.

When the application is installed using
npm install
I have a script that moves this from
.env.sample
to
.env
.

This causes an issue for me because when I'm testing and I run the install it overwrites my original
.env
. Which is normal behaviour, but I can't help but think there's a better way to do this to stop that from happening.

It's a pain because just before I push I have to rename
.env
to
.env.sample
and clear the development variables.

Any idea on how to do this so I don't have to manually rename the file before I push?

Here is the install script from
package.json
:

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.config.babel.js",
"start": "npm run build && electron .",
"postinstall": "bash postinstall.sh && mv .env.sample .env",
"add-cors": "add-cors-to-couchdb"
},


The
postinstall.sh
runs every time install runs to build some software. I could have just put the
mv
inside that, which is what I will probably do.

Answer

The simplest solution would be to not overwrite the file:

"postinstall": "bash postinstall.sh && cp -n .env.sample .env",

(note that I propose to use cp instead of mv so that you don't remove the default file)

Another (frequent) one would be to simply ask the user to do it. It's a light task compared to all the configuration he has to do anyway.

Comments