5

Mina is not able to load environment variables which are in database.yml file as mentioned below:

deploy.rb:

task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    queue! "#{rake} db:seed"
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'
  end
end

vim ~/.bash_profile :

export MYSQLUSERNAME=mysql_user_name
export MYSQLPASSWORD=mysql_password

database.yml:

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: my_db_name
  pool: 5
  username: <%= ENV['MYSQLUSERNAME'] %>
  password: <%= ENV['MYSQLPASSWORD'] %>

Error:

$ mina deploy
  .....
  .......
  -----> DB migrations unchanged; skipping DB migration
         $ RAILS_ENV="production" bundle exec rake db:seed
         rake aborted!
         Mysql2::Error: Access denied for user 'root'@'localhost' (using password: NO)
               /home/user_name/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.16/lib/mysql2/client.rb:70:in `connect'

Note: If i replace username(mysql_user_name) and password(mysql_user_name) values instead of <%= ENV['MYSQLUSERNAME'] %> and <%= ENV['MYSQLPASSWORD'] %> in database.yml file, it works well.

Can anyone please help me that how i can load ENV variables in *.yml file while deploying using Mina.

Thanks!

Peter Prabu
  • 1,128
  • 10
  • 20
  • Do you have those env variables on the target machine that you are deploying to? When you deploy, it will most probably just copy the database.yml file over to the server, and then try to start the application. In that case, the environment variables need to be available for that bash session, on the remote server. – Frost Jan 29 '15 at 15:01
  • @Frost Yes, i have those env variables on the target machine. The problem is while deploying the project itself throws an error. – Peter Prabu Jan 30 '15 at 05:00
  • Any updates here? – Ngoral Jul 09 '18 at 22:02
  • Oh, saw an issue on github. Would you like to post an answer here, please? – Ngoral Jul 09 '18 at 22:04

2 Answers2

2

Maybe late, but this may help another users.

For deployments I use Figaro gem, which allows to configure a set of environment variables in one YML file. These variables are used the same way.

For example:

YML:

MY_ENVIRONMENT_VARIABLE: 'Hi there!'

Application Ruby File:

<%= ENV['MY_ENVIRONMENT_VARIABLE'] %>

The advantage is that you can simply copy your file to the server with SCP command (assuming you are using *nix system) and better yet you can create a Mina task to do it.

Andrei Helo
  • 584
  • 5
  • 4
0

You need to put your ENV in .bashrc file. .bash_profile is not loaded as mina is using non-interactive shell.

seed here: https://stackoverflow.com/a/216204/1339894

Community
  • 1
  • 1
stef
  • 525
  • 6
  • 14