10

I am setting up a heroku and I am using using this guide to set up my local environment for development. I have a '.env' file in the root of my application with the variable set up as noted in the guide. When I run

heroku local:run rails runner "puts ENV['S3_BUCKET']"

it returns a blank. How do I get it to recognize/load the '.env' file?

npresco
  • 153
  • 1
  • 2
  • 11

3 Answers3

8

I recommend using https://github.com/bkeepers/dotenv gem if you want to load .env file with ENV variables. If you are already using it then according to dotenv readme you should load as soon as possible on boot, for example in config/application.rb add:

require 'dotenv'
Dotenv.load

If you use PUMA or Unicorn, in config/puma.rb, config/unicorn.rb add:

  require "dotenv"
  Dotenv.load("/path/to/env/.env")
midN
  • 567
  • 5
  • 8
  • 1
    I am using dotenv and PUMA as well. I thought you had cracked it but after adding both of those lines to my puma.rb and to application.rb for good measure, I am still getting nothing returned. Any other ideas? – npresco Dec 30 '15 at 22:33
1

.env file is meant for use in development and test environments. When using heroku you are talking about production environment. You don't want to put your .env file in your repo (it has your secrets in it people could possibly steal). So on heroku there isn't a .env file to reference ... Instead you now use Heroku's environment variables while naming the variables and their values EXACTLY as you did in the .env file then it will work

MilesStanfield
  • 4,571
  • 1
  • 21
  • 32
  • I am trying to use heroku in my local environment during development. I have put my .env file in .gitignore as well. – npresco Dec 30 '15 at 22:32
0

Had the same issue when following this guide.

The problem in my case was that I followed the guide link to https://devcenter.heroku.com/articles/s3 where they set environment variable S3_BUCKET_NAME (instead of S3_BUCKET) and when I came back to the guide I was trying to get S3_BUCKET, when what I had was S3_BUCKET_NAME

Mantas
  • 1,223
  • 1
  • 10
  • 15