简体   繁体   中英

Where to set environment variables such that they can be read from `config/initializer/*.rb`?

I have all my environment variables in config/initializers/app_environment_variables.rb and this setup has been working so far, except now I need to use the variables in config/environments.rb and config/environments/*.rb , which seem to be executed before config/initializer/*.rb .

I found this page showing "the configuration order" as the following,

  1. config/preinitializer.rb
  2. config/environment.rb
  3. config/environments/#{RAILS_ENV}.rb
  4. plugin initialization
  5. gem initialization
  6. config/initializer/*.rb
  7. ...

but that info is from 2009, whereas I am on Rails 3.1 . I expected to see this kind of info in the " initialization " or the " configuring " guides, but either I was unable to find the info, or it's not there.

I recommend using foreman and a .env file to set your environment variables.

  1. Create a .env.sample file that goes into source control
  2. Create your own .env file, and add .env to .gitignore
  3. Use foreman start rather than rails s
  4. enjoy the awesomeness (defaults to port 5000).

.env.sample

AWS_ACCESS_ID=YOUR_ACCESSKEY_GOES_HERE
ETC_ETC=ETC ETC ETC

Benefits:

  • You won't be committing your sensitive information to source control.
  • They truly will be environment variables, set before any code is ever run.

I added puts "--- #{__FILE__}" to the top of each of the following files in a new "demo" app ( rails new demo ):

config.ru
config/application.rb
config/boot.rb
config/environment.rb
config/environments/development.rb
config/initializers/hello.rb

Then I simply started the app with rails server , and got the following output ( .... for terseness):

--- /Volumes/..../initialization-order/demo/config/boot.rb
--- /Volumes/..../initialization-order/demo/config/application.rb
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
--- /Volumes/..../initialization-order/demo/config.ru
--- /Volumes/..../initialization-order/demo/config/environment.rb
--- /Volumes/..../initialization-order/demo/config/environments/development.rb
--- /Volumes/..../initialization-order/demo/config/initializers/hello.rb
[2013-05-16 15:05:59] INFO  WEBrick 1.3.1
[2013-05-16 15:05:59] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin12.3.0]
[2013-05-16 15:05:59] INFO  WEBrick::HTTPServer#start: pid=91029 port=3000
^C[2013-05-16 15:06:25] INFO  going to shutdown ...
[2013-05-16 15:06:25] INFO  WEBrick::HTTPServer#start done.
Exiting

Therefore the initialization order is as follows:

/config/boot.rb
/config/application.rb
/config.ru
/config/environment.rb
/config/environments/development.rb
/config/initializers/hello.rb

So if you want your environment variables loaded before config/environments/*.rb (and before /config/initializers/*.rb ) then read this next .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM