foreman fails to load environment variables

1.9k views Asked by At

I'm using rails 4.0.0 and ruby 2.0.0

When I start the server with foreman some of the environment variables fail to load. It really bugs me that some of the variables are loaded.

foreman start -e development.env

Procfile

web: bundle exec passenger start -p $PORT -e $RAILS_ENV
worker: bundle exec rake jobs:work RAILS_ENV=$RAILS_ENV

development.env file

S3_BUCKET=bucketname
AWS_ACCESS_KEY_ID=accesskey
AWS_SECRET_ACCESS_KEY=secretaccesskey
RAILS_ENV=development
PORT=3000

In my application.rb file i've added some logging to help debug this problem

puts "PORT is #{ENV["PORT"].inspect}"
puts "RAILS_ENV is #{ENV["RAILS_ENV"].inspect}"
puts "S3_BUCKET is #{ENV["S3_BUCKET"].inspect}"
puts "AWS_ACCESS_KEY_ID is #{ENV["AWS_ACCESS_KEY_ID"].inspect}"
puts "AWS_SECRET_ACCESS_KEY is #{ENV["AWS_SECRET_ACCESS_KEY"].inspect}"

Once I start the server this is the output for the logging code

23:34:52 worker.1 | PORT is nil
23:34:52 worker.1 | RAILS_ENV is "development"
23:34:52 worker.1 | S3_BUCKET is nil
23:34:52 worker.1 | AWS_ACCESS_KEY_ID is nil
23:34:52 worker.1 | AWS_SECRET_ACCESS_KEY is nil

Why oh Why ? :-(

When I load the rails console with foreman it successfully loads the variables

foreman run -e development.env rails c
1

There are 1 answers

0
Aetherus On

Try modify your development.env like

export S3_BUCKET=bucketname
export AWS_ACCESS_KEY_ID=accesskey
export AWS_SECRET_ACCESS_KEY=secretaccesskey
export RAILS_ENV=development
export PORT=3000

Then in the terminal

$ source /path/to/development.env
$ foreman start

Advanced

You can use dotenv to manage some of your environment variables without polluting your system environment. Though it can't manage those environment variables required for server booting like PORT.