Jeremiah
Jeremiah

Reputation: 106

Capistrano Devise.secret_key not set

When I try to deploy with Capistrano it gets to assets:precompile and I get this message:

01:08 deploy:assets:precompile
      01 ~/.rvm/bin/rvm default do bundle exec rake assets:precompile
      01 rake aborted!
      01 Devise.secret_key was not set. Please add the following to your Devise initializer:
      01
      01   config.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      01
      01 Please ensure you restarted your application after installing Devise or setting the key.

I don't want to add my secret key into devise.rb because I don't want it in version control. I am using an environment variable for secret_key_base which Devise.secret_key is supposed to fall back on. In fact, when I ssh into the server, manually navigate to the failed release and go into the rails console I find that Devise.secret_key works fine. It seems the only time it doesn't work is during the Capistrano deploy.

Edit: I can also manually run bundle exec rake assets:precompile just fine through ssh. The problem just seems to be Capistrano, but my environment variables are in /etc/environment so they should be loaded.

Upvotes: 1

Views: 482

Answers (2)

nzajt
nzajt

Reputation: 1985

You need to declare the ENV variable in your capistrano production file. Set an env variable on your computer(or the computer, or virtual machine where deploy is being called), then add the following code to your capistrano production file.

in config/deploy/production.rb

set :default_env, {
  "SECRET_KEY_BASE" => ENV['PRODUCTION_SECRET_KEY'],
 ....
}

You should always set env variable like this when using capistrano, because sometimes depending on the way you have capistrano set up, env variables on the server will not work, you could be using a different user to deploy, or you could be using rbenv or rvm. Whatever the reason, this is the best way to set env variables when using capistrano. And you don't have to set them on the server or keep them in version control.

Upvotes: 1

Nam Tran
Nam Tran

Reputation: 468

Reason

Capistrano use non-login, non-interactive shell modes, which doesn't load you environment variables. When you do ssh to server, it uses login, interactive shell modes, which loads your environment variables

Solution

Use another way to set environment variables. I often use Figaro and it works with Capistrano.

To prevent sensitive information are included in Git, add config/application.yml to Capistrano's linked_files and fill correct sensitive information to <shared directory>/config/application.yml on server

Upvotes: 0

Related Questions