Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoked tasks don't pick up ENV variables #11

Closed
razielgn opened this issue Jun 13, 2012 · 9 comments
Closed

Invoked tasks don't pick up ENV variables #11

razielgn opened this issue Jun 13, 2012 · 9 comments

Comments

@razielgn
Copy link

I have the following .bash_profile on the server:

export MONGODB_URI="mongodb://localhost/db"

However when assets get precompiled an thus the Rails app gets loaded I get

rake aborted!
db_name must be a string or symbol

Tasks: TOP => environment
(See full trace by running task with --trace)
rake aborted!

Meaning mongodb's ENV variable isn't picked up.
When I force mina to invoke source ~/.bash_profile && rake assets:precompile everything goes right.

@razielgn
Copy link
Author

Solved by invoking a task outside deploy as seen in #5.

task :env do                                                                                                                                                                                                
  queue %{
    echo "-----> Loading environment"
    #{echo_cmd %[source ~/.bash_profile]}
  }
end

desc 'Deploys the current version to the server.'
task :deploy do
  invoke :env

  deploy do
    invoke :'git:clone'
    invoke :'bundle:install'
    invoke :'rails:assets_precompile'
    invoke :'mongoid:create_indexes'
  end 
end

@rstacruz
Copy link
Member

Doing mina deploy --simulate may help in debugging. I have no idea what's going on--I'd imagine mongoid:create_indexes is something that only invokes a rake task, and that should be able to pick up the ENV vars just fine.

If I were to make a really-lucky-shot-in-the-dark guess, your mongoid config probably looks like:

development:
  # stuff
production:
  uri: <%= ENV['MONGODB_URI'] %>

But you're invoking rake like so:

bundle exec rake mongoid:create_indexes

...which will probably pick up the development config, not the production config. You should try #{rake} db:mongoid:create_indexes instead, as #{rake} by default expands to RAILS_ENV=production bundle exec.

@rstacruz
Copy link
Member

You can verify that the env vars are picked up like so:

deploy do
  queue "echo -----> Mongo URI is $MONGODB_URI"
  ...
end

@rstacruz
Copy link
Member

...out of sheer curiosity, why are you reindexing after every deploy? Shouldn't you only perform a reindex if you change the fields that are being indexed? :)

@razielgn
Copy link
Author

But you're invoking rake like so:

bundle exec rake mongoid:create_indexes

...which will probably pick up the development config, not the production config.

I have set the RAILS_ENV=production variable in .bash_profile.
Anyways, the ENV vars are in fact picked up when invoking the :env command outside the deploy block.

...out of sheer curiosity, why are you reindexing after every deploy? Shouldn't you only perform a reindex if you change the fields that are being indexed? :)

Invoking that command executes ensureIndex(...) which creates the index if it doesn't already exists, otherwise it will do nothing.

I've managed to get the deploy script working but I had found some friction with Unicorn. I won't be able to work on it this coming week, but I'll post updates as soon as I can.

@rstacruz
Copy link
Member

Cool, looks like there's nothing wrong with ENV vars so I'm closing this. Feel free to open another issue if you encounter problems. Cheers! :)

@matheusvetor
Copy link

I was trying to deploy but my server is behind a proxy. I used the @razielgn hook to set env http_proxy variable and it worked. Thanks!

task :env do
  queue %{
    echo "-----> Seting environment variables"
    #{echo_cmd %[export http_proxy=http://proxy-ip:port]}
    #{echo_cmd %[export https_proxy=http://proxy-ip:port]}
  }
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  invoke :env

  deploy do
    invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'

    to :launch do
      # invoke :'sidekiq:restart'
      queue "touch #{deploy_to}/shared/tmp/restart.txt"
    end
  end
end

@diegocastro
Copy link

Thanks @matheusvetor it saved my symfony deploy. There should be an easier way to do this.

@benbonnet
Copy link

👍 💯
Got into this problem using sidekiq, it was not picking up the environment variables.
Saved me a lot of time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants