7
task :some_task, :environment do |t, args|
  puts Rails.env #=> development, production, etc
  puts ENV #=> {}
end

I set some environment variables (either via a local .env, or via Heroku Config via Herokusan), such as which AWS bucket to use, and I want to reference them in the rake task, but ENV is an empty hash. I know something related to environment gets done because of the :environment task dependency and that Rails.env has a value, but I'm not clear on the details.

So, how can I use ENV in a Rake task?

Narfanator
  • 5,595
  • 3
  • 39
  • 71
  • How are you running this task? ENV is a core Ruby thing, so it should always reflect your available environmental variables. – Tim Dorr Mar 28 '13 at 19:39
  • 2
    `$ rake some_task` in the app directory. Some of `ENV` is in `.env`, and I may need to do `$ foreman run rake task` for the Heroku stuff. – Narfanator Mar 28 '13 at 20:21
  • 1
    @Narfanator you should answer this as an actual answer so I can upvote you. Doing `$ foreman run rake some_task` is the correct answer – Andrew Gertig Apr 22 '13 at 02:53

2 Answers2

6

Two good ways to do it:

Use Heroku's "Foreman" tool. Put all your environment variables into .env:

VAR=value

and run foreman run rake some_task.

Or (and, I'd recommend this way), using the "Figaro" gem. Put your vars into config/application.yml:

VAR: value

and that's it; rake some_task.

I'd recommend the latter, if only because rake figaro:heroku will push your env up as it's specified in application.yml

Narfanator
  • 5,595
  • 3
  • 39
  • 71
  • 1
    I know this is an old question, but how does putting them in a .env file or application.yml keep your passwords / keys out of the codebase? I thought that was an important reason for using ENV variables. Isn't there some call we can make from Rake which will fetch them? – JosephK Apr 08 '15 at 06:45
  • You (generally) don't check your application.yml or .env into the repo; that's what I meant by "codebase", and putting them directly into the host computer's ENV runs into its own issues. My question *is* basically asking for some call we can make from Rake, but I didn't find one. – Narfanator Apr 18 '15 at 21:43
  • More experienced devs at my company decided to use this pattern: `config = YAML.load("application.yml")`, but they also decided not to explain why. YMMV; I use it in some situations (ie, sinatra) and not in others (ie, rails). – Narfanator Apr 18 '15 at 21:45
  • Good to know. Thanks for the update. Sometimes a workaround is the best you can do. – JosephK Apr 23 '15 at 07:52
0

Just in case somebody searches for another decision. I had a trouble getting ENV variable within rake task on production server. So I had application.yml file, but ENV['variable'] was just blank

My original command:

/usr/local/rvm/bin/rvm use 2.5.1 do bundle exec rake upload_source_maps_to_rollbar

Adding RAILS_ENV=production solved the issue:

RAILS_ENV=production /usr/local/rvm/bin/rvm use 2.5.1 do bundle exec rake upload_source_maps_to_rollbar
vekozlov
  • 664
  • 9
  • 19