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 "root@xxx.xxx.xxx.xx" 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.
You can create a file in your server called
application.yml
inshared/config
.Choose any one of solution from bellow
Following code in deploy.rb will automatically symlink your
application.yml
Or
Then symlink this
application.yml
with yourcurrent/config/application.yml
with a simple capistrano task.Give up on environment vars and just simply read a file. Do this in secrets.yml.
Then make the secret file.
Before settling on this solution I tried exporting the secret from my .bash_profile, .profile, and .bashrc (top and bottom). I also tried PermitUserEnvironment and .ssh/environment. I have no idea how an environment variable gets from "the environment" into a capistrano deployment. I ran ssh example.com printenv and saw my vars. I logged in and saw my vars. But puma started with capistrano... it always has it's own environment.
Rather than exporting env variables in deploy.rb, use dotenv to load environment variables from
.env[.<staging>]
into ENV when rails booting.Follow these steps:
Add this line to the top of your Gemfile:
Put
.env.production
file into thelinked_file
indeploy/production.rb
:On the remote host, add the
.env.production
file inshared
folder which contains this line:There is a gem for exactly this task: https://github.com/capistrano-plugins/capistrano-secrets-yml
Install
Add this to Gemfile:
And then:
$ bundle install
Setup and usage
populate production secrets in local config/secrets.yml:
add to Capfile:
create secrets.yml file on the remote server by executing this task:
You can now proceed with other deployment tasks.