Skip to content

Instantly share code, notes, and snippets.

@bertBruynooghe
Last active November 28, 2017 10:26
Show Gist options
  • Save bertBruynooghe/3b72a414df4bb9cab0c25aeaf8e52296 to your computer and use it in GitHub Desktop.
Save bertBruynooghe/3b72a414df4bb9cab0c25aeaf8e52296 to your computer and use it in GitHub Desktop.
usage of .env

Usage of dotenv

Gemfile

Make sure dotenv is not included for production:

    group :development, :test do
      gem 'dotenv-rails'
    end

.env files

  • .env file is always loaded.
  • .env.development is loaded in development. It should contain overrides/extension on .env, and is maybe not always necessary (see further).
  • .env.test is used when running in test mode, and contains overrides/extension on .env.
    • env.local can be used to override any of the other settings, and it should not be checked in.
  • If you want more env file alternatives, there are several approaches:
    • for tests only: you might consider adding loading of env.test.local to the spec_helper.rb
    • you might use a custom environment RAILS_ENV=custom rails s and have a custom .env file too: env.custom. (This might also require you to alter database.yml a little, or make sure you also define DATABASE_URL in the .env file)
    • usage of dotenv command line: dotenv -f <custom .env file> <command>

Note

Generally, it is a code smell if you do not have .env. checked in to your repository, since that probably means you're duplicating entries in the .env.test and .env.development (or at least don't make it easy to remove duplicates). I also tend to have only two env files in the project (with one being .env), as you'll only have to discriminate between two.

Services

The services referred to in .env files should require no extra confguration (except from rake db:seed) to get the project running, and for testing it is highly recommended that they run in isolation (all-in-one locally, any kind of partitioning on CI).

Continuous Integration

On CI, we create the .env.local file in the script with overrides that are necessary on CI.

Example: echo export DATABASE_URL="xxx" >> .env.local

For things that don't change often, we might create a .env.test.ci, and add that to the env.local too:

cat .env.test.ci > .env.local

Remarks

You must ensure you use the correct version of doten-rails, especially when rails >= 4.2. (2.0.1 is NOT working correctly there). Unfortunately, that also mean you cannot combine it properly with foreman 0.76.0 anymore...

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