dotenv-rails 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +56 -14
- data/lib/dotenv/rails-now.rb +1 -1
- data/lib/dotenv/rails.rb +28 -8
- metadata +7 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7e3538ec3b2d9ddf456640c6bbc5784de19204b8
|
|
4
|
+
data.tar.gz: 1da62894cf429e9c10bc168478d023f7b84dfcb0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a8391f94ba1cf3af34a577925094cc1444ade302de874d7bf9573a335e22e3c7dd828e5a89d1b75f48821891de03a00bab66df921a33e9c45d3b0f15110139c8
|
|
7
|
+
data.tar.gz: aa4b640e4f01d91b46bcfb38ad1c8ef312f4c0b1a0af070f7373844676d86fa53f9ed23224583ac6091691563fb3a3b543348bd824ef1e5e7596eba27ebdb41e
|
data/README.md
CHANGED
|
@@ -15,7 +15,7 @@ But it is not always practical to set environment variables on development machi
|
|
|
15
15
|
Add this line to the top of your application's Gemfile:
|
|
16
16
|
|
|
17
17
|
```ruby
|
|
18
|
-
gem 'dotenv-rails', :
|
|
18
|
+
gem 'dotenv-rails', groups: [:development, :test]
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
And then execute:
|
|
@@ -40,7 +40,7 @@ HOSTNAME = ENV['HOSTNAME']
|
|
|
40
40
|
If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotenv/rails-now`.
|
|
41
41
|
|
|
42
42
|
```ruby
|
|
43
|
-
gem 'dotenv-rails', :
|
|
43
|
+
gem 'dotenv-rails', require: 'dotenv/rails-now'
|
|
44
44
|
gem 'gem-that-requires-env-variables'
|
|
45
45
|
```
|
|
46
46
|
|
|
@@ -55,10 +55,20 @@ $ gem install dotenv
|
|
|
55
55
|
As early as possible in your application bootstrap process, load `.env`:
|
|
56
56
|
|
|
57
57
|
```ruby
|
|
58
|
+
require 'dotenv/load'
|
|
59
|
+
|
|
60
|
+
# or
|
|
58
61
|
require 'dotenv'
|
|
59
62
|
Dotenv.load
|
|
60
63
|
```
|
|
61
64
|
|
|
65
|
+
By default, `load` will look for a file called `.env` in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
require 'dotenv'
|
|
69
|
+
Dotenv.load('file1.env', 'file2.env')
|
|
70
|
+
```
|
|
71
|
+
|
|
62
72
|
Alternatively, you can use the `dotenv` executable to launch your application:
|
|
63
73
|
|
|
64
74
|
```shell
|
|
@@ -70,7 +80,7 @@ To ensure `.env` is loaded in rake, load the tasks:
|
|
|
70
80
|
```ruby
|
|
71
81
|
require 'dotenv/tasks'
|
|
72
82
|
|
|
73
|
-
task :
|
|
83
|
+
task mytask: :dotenv do
|
|
74
84
|
# things that require .env
|
|
75
85
|
end
|
|
76
86
|
```
|
|
@@ -84,30 +94,51 @@ S3_BUCKET=YOURS3BUCKET
|
|
|
84
94
|
SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
85
95
|
```
|
|
86
96
|
|
|
97
|
+
Whenever your application loads, these variables will be available in `ENV`:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
config.fog_directory = ENV['S3_BUCKET']
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
You may also add `export` in front of each line so you can `source` the file in bash:
|
|
104
|
+
|
|
105
|
+
```shell
|
|
106
|
+
export S3_BUCKET=YOURS3BUCKET
|
|
107
|
+
export SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Multi-line values
|
|
111
|
+
|
|
87
112
|
If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines:
|
|
88
113
|
|
|
89
114
|
```shell
|
|
90
115
|
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9…\n-----END DSA PRIVATE KEY-----\n"
|
|
91
116
|
```
|
|
92
117
|
|
|
118
|
+
### Command Substitution
|
|
119
|
+
|
|
93
120
|
You need to add the output of a command in one of your variables? Simply add it with `$(your_command)`:
|
|
121
|
+
|
|
94
122
|
```shell
|
|
95
123
|
DATABASE_URL="postgres://$(whoami)@localhost/my_database"
|
|
96
124
|
```
|
|
97
125
|
|
|
98
|
-
|
|
126
|
+
### Variable Substitution
|
|
127
|
+
|
|
128
|
+
You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unqoted or double-quoted values.
|
|
99
129
|
|
|
100
130
|
```shell
|
|
101
|
-
|
|
102
|
-
export SECRET_KEY=YOURSECRETKEYGOESHERE
|
|
131
|
+
DATABASE_URL="postgres://${USER}@localhost/my_database"
|
|
103
132
|
```
|
|
104
133
|
|
|
105
|
-
|
|
134
|
+
If a value contains a `$` and it is not intended to be a variable, wrap it in single quotes.
|
|
106
135
|
|
|
107
|
-
```
|
|
108
|
-
|
|
136
|
+
```shell
|
|
137
|
+
PASSWORD='pas$word'
|
|
109
138
|
```
|
|
110
139
|
|
|
140
|
+
### Comments
|
|
141
|
+
|
|
111
142
|
Comments may be added to your file as such:
|
|
112
143
|
|
|
113
144
|
```shell
|
|
@@ -116,24 +147,35 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
|
|
|
116
147
|
SECRET_HASH="something-with-a-#-hash"
|
|
117
148
|
```
|
|
118
149
|
|
|
119
|
-
|
|
150
|
+
## Frequently Answered Questions
|
|
120
151
|
|
|
121
|
-
|
|
152
|
+
### Can I use dotenv in production?
|
|
122
153
|
|
|
123
154
|
dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
|
|
124
155
|
|
|
125
156
|
However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
|
|
126
157
|
|
|
127
|
-
|
|
158
|
+
If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
|
|
128
159
|
|
|
129
|
-
|
|
160
|
+
### What other .env* files can I use?
|
|
130
161
|
|
|
131
|
-
|
|
162
|
+
`dotenv-rails` will load the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:
|
|
163
|
+
|
|
164
|
+
- `.env` - The Original®
|
|
165
|
+
- `.env.development`, `.env.test`, `.env.production` - Environment-specific settings.
|
|
166
|
+
- `.env.local` - Local overrides. This file is loaded for all environments _except_ `test`.
|
|
167
|
+
- `.env.development.local`, `.env.test.local`, `.env.production.local` - Local overrides of environment-specific settings.
|
|
168
|
+
|
|
169
|
+
### Should I commit my .env file?
|
|
132
170
|
|
|
133
171
|
Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
|
|
134
172
|
|
|
135
173
|
Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data.
|
|
136
174
|
|
|
175
|
+
### Why is it not overriding existing `ENV` variables?
|
|
176
|
+
|
|
177
|
+
By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.overload`.
|
|
178
|
+
|
|
137
179
|
## Contributing
|
|
138
180
|
|
|
139
181
|
If you want a better idea of how dotenv works, check out the [Ruby Rogues Code Reading of dotenv](https://www.youtube.com/watch?v=lKmY_0uY86s).
|
data/lib/dotenv/rails-now.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and
|
|
3
3
|
# require `dotenv/rails-now`.
|
|
4
4
|
#
|
|
5
|
-
# gem "dotenv-rails", :
|
|
5
|
+
# gem "dotenv-rails", require: "dotenv/rails-now"
|
|
6
6
|
# gem "gem-that-requires-env-variables"
|
|
7
7
|
#
|
|
8
8
|
|
data/lib/dotenv/rails.rb
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
require "dotenv"
|
|
2
2
|
|
|
3
|
+
# Fix for rspec rake tasks loading in development
|
|
4
|
+
#
|
|
5
|
+
# Dotenv loads environment variables when the Rails application is initialized.
|
|
6
|
+
# When running `rake`, the Rails application is initialized in development.
|
|
7
|
+
# Rails includes some hacks to set `RAILS_ENV=test` when running `rake test`,
|
|
8
|
+
# but rspec does not include the same hacks.
|
|
9
|
+
#
|
|
10
|
+
# See https://github.com/bkeepers/dotenv/issues/219
|
|
11
|
+
if defined?(Rake.application)
|
|
12
|
+
is_running_specs = Rake.application.top_level_tasks.grep(/^spec(:|$)/).any?
|
|
13
|
+
Rails.env = ENV["RAILS_ENV"] ||= "test" if is_running_specs
|
|
14
|
+
end
|
|
15
|
+
|
|
3
16
|
Dotenv.instrumenter = ActiveSupport::Notifications
|
|
4
17
|
|
|
5
18
|
# Watch all loaded env files with Spring
|
|
6
19
|
begin
|
|
7
|
-
require "spring/
|
|
20
|
+
require "spring/commands"
|
|
8
21
|
ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args|
|
|
9
22
|
event = ActiveSupport::Notifications::Event.new(*args)
|
|
10
23
|
Spring.watch event.payload[:env].filename if Rails.application
|
|
@@ -17,18 +30,12 @@ module Dotenv
|
|
|
17
30
|
# Dotenv Railtie for using Dotenv to load environment from a file into
|
|
18
31
|
# Rails applications
|
|
19
32
|
class Railtie < Rails::Railtie
|
|
20
|
-
config.before_configuration { load }
|
|
21
|
-
|
|
22
33
|
# Public: Load dotenv
|
|
23
34
|
#
|
|
24
35
|
# This will get called during the `before_configuration` callback, but you
|
|
25
36
|
# can manually call `Dotenv::Railtie.load` if you needed it sooner.
|
|
26
37
|
def load
|
|
27
|
-
Dotenv.load(
|
|
28
|
-
root.join(".env.local"),
|
|
29
|
-
root.join(".env.#{Rails.env}"),
|
|
30
|
-
root.join(".env")
|
|
31
|
-
)
|
|
38
|
+
Dotenv.load(*dotenv_files)
|
|
32
39
|
end
|
|
33
40
|
|
|
34
41
|
# Internal: `Rails.root` is nil in Rails 4.1 before the application is
|
|
@@ -43,5 +50,18 @@ module Dotenv
|
|
|
43
50
|
def self.load
|
|
44
51
|
instance.load
|
|
45
52
|
end
|
|
53
|
+
|
|
54
|
+
config.before_configuration { load }
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def dotenv_files
|
|
59
|
+
[
|
|
60
|
+
root.join(".env.#{Rails.env}.local"),
|
|
61
|
+
(root.join(".env.local") unless Rails.env.test?),
|
|
62
|
+
root.join(".env.#{Rails.env}"),
|
|
63
|
+
root.join(".env")
|
|
64
|
+
].compact
|
|
65
|
+
end
|
|
46
66
|
end
|
|
47
67
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dotenv-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brandon Keepers
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2017-01-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: dotenv
|
|
@@ -16,21 +16,21 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - '='
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 2.
|
|
19
|
+
version: 2.2.0
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - '='
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 2.
|
|
26
|
+
version: 2.2.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: railties
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
33
|
+
version: '3.2'
|
|
34
34
|
- - "<"
|
|
35
35
|
- !ruby/object:Gem::Version
|
|
36
36
|
version: '5.1'
|
|
@@ -40,7 +40,7 @@ dependencies:
|
|
|
40
40
|
requirements:
|
|
41
41
|
- - ">="
|
|
42
42
|
- !ruby/object:Gem::Version
|
|
43
|
-
version: '
|
|
43
|
+
version: '3.2'
|
|
44
44
|
- - "<"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
46
|
version: '5.1'
|
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
90
90
|
version: '0'
|
|
91
91
|
requirements: []
|
|
92
92
|
rubyforge_project:
|
|
93
|
-
rubygems_version: 2.
|
|
93
|
+
rubygems_version: 2.5.2
|
|
94
94
|
signing_key:
|
|
95
95
|
specification_version: 4
|
|
96
96
|
summary: Autoload dotenv in Rails.
|