Search code examples
ruby-on-railsnginxcapistranodigital-oceanpuma

Deploying rails app to DigitalOcean using capistrano, nginx, puma


I am currently following the tutorial on DigitalOcean's page on how to deploy to production using capistrano. I have gotten to the step where it tries to run the migration but it blows up giving this error:

SSHKit::Runner::ExecuteError: Exception while executing on host 104.236.51.1: rake exit status: 1 rake stdout: rake aborted! Mysql2::Error: Access denied for user 'foo'@'localhost' (using password: NO)

Looking at the logs I can see the command it tries executing, which looks like: /home/foo/apps/programming_class_judge/releases/20161102041809 && ( export RAILS_ENV="production" ; ~/.rvm/bin/rvm default do bundle exec rake db:migrate )

If I ssh into the machine and run this command, it works. I don't understand why it is not using the password when deploying with capistrano.

Here is what the prod settings my db.yaml file looks like(the JUDGE_PRODUCTION_DB_PASSWORD environment variable is in both my local and remote machines): default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: <= ENV['JUDGE_DB_PASSWORD'] %> socket: /tmp/mysql.sock production: <<: *default database: judge_production username: foo password: <%= ENV['JUDGE_PRODUCTION_DB_PASSWORD'] %> socket: /var/run/mysqld/mysqld.sock My config/deploy.rb script is basically a copy-paste of what can be found on this tutorial, substituting the text in red: https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma

Anyone have any idea of why the migration might be failing when I do cap production deploy:initial, but not when I try it manually?

Any help is appreciated.


Solution

  • Add your environment variables in /etc/environment. Adding to .bashrc, .bashprofile, .profile adds them as shell variables and not environment and capistrano does not really create a new shell.

    There is also a set :default_env, { 'var' => 'val' } option for capistrano.

    Refer to this answer for more info: Capistrano and environment variables