17

When I start

cap production deploy

it fails like this:

DEBUG [4ee8fa7a] Command: cd /home/deploy/myapp/releases/releases/20131025212110 && (RVM_BIN_PATH=~/.rvm/bin RAILS_ENV= ~/.rvm/bin/myapp_rake assets:precompile )
DEBUG [4ee8fa7a]        rake aborted!
DEBUG [4ee8fa7a]        database configuration does not specify adapter

You can see that "RAILS_ENV=" is actually empty and I'm wondering why that might be happening? I assume that this is the reason for the latter error that I don't have a database configuration.

The deploy.rb file is below:

set :application, 'myapp'
set :repo_url, 'git@github.com:developer/myapp.git'
set :branch, :master
set :deploy_to, '/home/deploy/myapp/releases'
set :scm, :git
set :devpath, "/home/deploy/myapp_development"
set :user, "deploy"
set :use_sudo, false
set :default_env, { rvm_bin_path: '~/.rvm/bin' }

set :keep_releases, 5

namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      within release_path do
        execute " bundle exec thin restart -O -C config/thin/production.yml"
      end
    end
  end

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      within release_path do

      end
    end
  end

  after :finishing, 'deploy:cleanup'
end

Database.yml:

production:
  adapter: mysql2
  encoding: utf8
  database: myapp_production
  pool: 5
  username: user
  password: pass
  host: localhost

development:
  adapter: mysql2
  encoding: utf8
  database: myapp_development
  pool: 5
  username: user
  password: pass
  host: localhost

The issue is resolved if I add

set :rails_env, "production"

to my deploy.rb, but this looks like hardcoding to me and I'm sure there's a nicer solution.

Kazmin
  • 1,003
  • 1
  • 10
  • 26
  • Can you post your `database.yml`? It should contain a section for `production` with database adapter. – R Milushev Oct 25 '13 at 22:06
  • I added it to the description. – Kazmin Oct 25 '13 at 22:30
  • I would suggest to double check in your `Gemfile` if the declaration for `gem "mysql2"` is inside the `group :production do ... end`. – R Milushev Oct 25 '13 at 22:36
  • 1
    It's not in a group at all. The problem is not in the database adapter, it's in the capistrano settings: RAILS_ENV= ~/.rvm/bin/myapp_rake assets:precompile. Rails_Env should not be empty running this. – Kazmin Oct 25 '13 at 22:51
  • I think `RAILS_ENV` is not empty, but the wrong argument is assigned to it. Take a look at this line on your `deploy.rb` : `set :default_env, { rvm_bin_path: '~/.rvm/bin' }` . Here you pass a hash to your `default_env` variable. Try to assign simply a value you need, something like : `set :default_env, 'production'`. – R Milushev Oct 25 '13 at 23:02
  • But it seems you've found the solution yourself... – R Milushev Oct 25 '13 at 23:04
  • 1
    but it's a hardcode solution which I don't like very much, capistrano should be able to get the environment from the "cap production deploy" and set it properly ... – Kazmin Oct 26 '13 at 14:21
  • I guess there is a little confusion about the term `environment` in your case. As you can see [here](http://rvm.io/deployment/capistrano), hard coded `default_environment` is representation of rvm Ruby version. Your error is caused by wrong `RAILS_ENV`. Try to change the name of your var: `default_environment`, not `default_env`. – R Milushev Oct 26 '13 at 15:24
  • Actually for capistrano v3, which is the one I'm using, it's default_env. See the commit comments: [github](https://github.com/capistrano/capistrano/pull/564) – Kazmin Oct 26 '13 at 17:29
  • You are absolutely right. It would be interesting for me the solution of this case. – R Milushev Oct 26 '13 at 18:31

6 Answers6

17

Edit: Per this pull request, it's now fixed in version 1.1.0 of capistrano-rails.

Per this Github issue, another fix is to edit your Capfile. Comment out these two lines

#require 'capistrano/rails/assets'
#require 'capistrano/rails/migrations'

and put this line in

require 'capistrano/rails'

which will correctly set your RAILS_ENV variable.

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
13

Using Cap 3 and capistrano_rails on rails 4 I was getting the same error; in the environment file(s) being deployed, I set

set :stage, :production
set :rails_env, 'production' # even though doc says only need to do this if it's different

Doc here: https://github.com/capistrano/rails

Tom Harrison
  • 13,533
  • 3
  • 49
  • 77
9

Based on Marc's answer which definitely seems to be the right one,

you can workaround this until it is fixed upstream by adding this to your config/deploy.rb in the "namespace :deploy" block:

  desc 'Provision env before assets:precompile'
  task :fix_bug_env do
    set :rails_env, (fetch(:rails_env) || fetch(:stage))
  end

  before "deploy:assets:precompile", "deploy:fix_bug_env"

This will force loading the env and provisionning RAILS_ENV before assets:precompile is called.

Root 42
  • 91
  • 1
8

Seems to be a bug in capistrano-rails.

There is a task (rails.rake) that sets the environment either from rails_env or stage:

namespace :deploy do
  before :starting, :set_rails_env do
    set :rails_env, (fetch(:rails_env) || fetch(:stage))
  end
end

But this task isn't called before i.e. assets:precompile. So this:

namespace :assets do
  task :precompile do
    on roles :web do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "assets:precompile"
        end
      end
    end
  end
end

fails because rails_env is nil if it isn't set explicitly.

If I have the time to dig a little deeper I'll file a bug report.

Marc
  • 183
  • 7
3

If you're using passenger, you need to add

rails_env production;

in the in web server's (eg: nginx) .conf where you've specified values for passenger_ruby and passenger_root.

Rahul
  • 1,866
  • 17
  • 32
1

What happens if you add a file:

deploy/production.rb

With this line:

set :stage, :production
Ben
  • 432
  • 5
  • 16