Invoked tasks don't pick up ENV variables #11

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

Comments

Projects
None yet
5 participants
@razielgn

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

This comment has been minimized.

Show comment
Hide comment
@razielgn

razielgn Jun 13, 2012

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

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

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Jun 15, 2012

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.

Member

rstacruz commented Jun 15, 2012

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

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Jun 15, 2012

Member

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

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

rstacruz commented Jun 15, 2012

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

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

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Jun 15, 2012

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? :)

Member

rstacruz commented Jun 15, 2012

...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

This comment has been minimized.

Show comment
Hide comment
@razielgn

razielgn Jun 15, 2012

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.

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

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Jun 15, 2012

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! :)

Member

rstacruz commented Jun 15, 2012

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! :)

@rstacruz rstacruz closed this Jun 15, 2012

@matheusvetor

This comment has been minimized.

Show comment
Hide comment
@matheusvetor

matheusvetor Feb 27, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@diegocastro

diegocastro Oct 2, 2015

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

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

@benbonnet

This comment has been minimized.

Show comment
Hide comment
@benbonnet

benbonnet Mar 7, 2016

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

👍 💯
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