Search code examples
ruby-on-railsrubyforeman

Setting rails environment with Foreman


How do you set the rails environment with Foreman?

For example, if I wanted to run bundle exec rake assets:precompile on my development machine, but I wanted to set the rails environment to production so I could see exactly how this process would run on the production server - I could do this using vanilla rails by doing RAILS_ENV=production bundle exec rake assets:precompile.

But how do I do this with Foreman? For example, I know I can do bundle exec foreman run rake assets:precompile, but that doesn't specify the rails environment as production.


Solution

  • The solution is to use a .env file in your project's root directory. Inside that file, add this line:

    RAILS_ENV=production
    

    Now, anytime you run Foreman locally, it will set the rails environment to production. So, if you run bundle exec foreman run rake assets:precompile, the environment will be production for that task. Don't forget to change it back when you're done! ;-)