Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up secret_key_base on deploy with Capistrano 3

So I'm deploying to a Ubuntu droplet hosted on DigitalOcean a Rails 4 application running on Apache and Phusion Passenger. After deployment I've been getting 500s as a result of Rails production not finding the secret_key_base token for production. However, if I run an echo $SECRET_KEY_BASE it returns the rake secret generated by my deploy.rb.

The deploy.rb task to set that up is:

namespace :deploy do
  task :start do ; end
  task :stop do ; end

  desc "Setup ENV variables"
  task :env_vars do
    on "[email protected]" do
      execute "export SECRET_KEY_BASE=#{`bundle exec rake secret`}"
    end
  end
end

before "deploy", "deploy:env_vars"

However, Rails is still not picking it up. I even ssh'd into my server and in rails console checked and ENV["SECRET_KEY_BASE"] returns the correct secret token.

I thought using Capistrano's :default_env would work, but that only seems to set up environmental variables for the deploy task, but not actually on the server. Is there any easy way to solve this solution? My fallback is to just place the secret within secrets.yml since the repo is private, but I rather not do that.

like image 832
Fasih Awan Avatar asked Feb 10 '23 22:02

Fasih Awan


2 Answers

There is a gem for exactly this task: https://github.com/capistrano-plugins/capistrano-secrets-yml

Install

Add this to Gemfile:

group :development do
  gem 'capistrano', '~> 3.2.1'
  gem 'capistrano-secrets-yml', '~> 1.0.0'
end

And then:

$ bundle install

Setup and usage

make sure your local config/secrets.yml is not git tracked. It should be on the disk, but gitignored.

populate production secrets in local config/secrets.yml:

production:
  secret_key_base: d6ced...

add to Capfile:

require 'capistrano/secrets_yml'

create secrets.yml file on the remote server by executing this task:

$ bundle exec cap production setup

You can now proceed with other deployment tasks.

like image 80
Joe Eifert Avatar answered Feb 13 '23 13:02

Joe Eifert


You can create a file in your server called application.yml in shared/config.

Choose any one of solution from bellow

Following code in deploy.rb will automatically symlink your application.yml

set :linked_files, %w{config/application.yml}

Or

Then symlink this application.yml with your current/config/application.yml with a simple capistrano task.

like image 33
Rokibul Hasan Avatar answered Feb 13 '23 13:02

Rokibul Hasan