Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failing to find ENV['SECRET_KEY'] in devise setup on heroku

This gist on not version-controlling your secret keys is awesome, and I've used it a few times now for no-longer versioning my rails secret-key-base.

I tried using it for the devise secret_key on heroku and my attempt failed miserably. It works fine in dev, but refused to allow me to push to heroku - saying the devise key which I created (in the same way as in the gist above) wasn't set.

I got it working with a hard-coded secret-key (checked into git), but not when I used the following:

Devise.setup do |config|
  config.secret_key = ENV['DEVISE_SECRET_KEY']
...

(with associated environment variable triple-checked that it was there)

It seems to fail on precompiling assets during the push to heroku

$  git push heroku master
     ... (bundle stuff here)
       Running: rake assets:precompile
       rake aborted!
       Devise.secret_key was not set. Please add the following to your Devise initializer:
       config.secret_key = '0cfa796871e0...
   /tmp/build_.../vendor/bundle/ruby/1.9.1/gems/devise-3.1.1/lib/devise/rails/routes.rb:446:in `raise_no_secret_key'
   /tmp/build_.../vendor/bundle/ruby/1.9.1/gems/devise-3.1.1/lib/devise/rails/routes.rb:195:in `devise_for'
   /tmp/build_.../config/routes.rb:2:in `block in <top (required)>'
    ...( rest of the long stacktrace with little of interest here)

the error is being raised when "devise_for" is run in the routes directory. relevant line:

MyApp::Application.routes.draw do
  devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout'}

The relevant line in the actual devise gem is:

      raise_no_secret_key unless Devise.secret_key

So it really is just checking if the secret_key got set.

just to confirm... I checked the heroku config, and I did in fact put a secret key into the environment under that name.

DEVISE_SECRET_KEY:            3f844454bee...(more here)
RAILS_SECRET_KEY_BASE:        04bf569d4e...(more here)

because it's in a rake task instead of the app - I'm guessing that's why it can't get to the ENV???

any ideas on where I might begin looking for a solution?

like image 689
Taryn East Avatar asked Nov 07 '13 09:11

Taryn East


2 Answers

You're wanting to use a config var before (at compile time) it's actually made available to your app (at slug compilation time).

Try switching on the Heroku Labs: user-env-compile

I suspect it will solve the problem.

like image 104
Jon Mountjoy Avatar answered Oct 21 '22 06:10

Jon Mountjoy


If it's breaking at compile time you need to enable user-env-compile, heroku labs:enable user-env-compile to make the environment available when the application boots to compile the assets.

like image 34
John Beynon Avatar answered Oct 21 '22 05:10

John Beynon