Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Mina - not able to load environment variables in database.yml file

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!

like image 376
Peter Prabu Avatar asked Oct 20 '22 18:10

Peter Prabu


1 Answers

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.

like image 130
Andrei Helo Avatar answered Nov 03 '22 06:11

Andrei Helo