Capistrano 3,Rails 4,数据库配置没有指定适配器

当我开始

上限生产部署

它失败了:

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 

您可以看到“RAILS_ENV =”实际上是空的,我想知道为什么会发生这种情况? 我假设这是后一个错误的原因,我没有数据库配置。

deploy.rb文件如下:

 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 

如果我添加,问题就解决了

 set :rails_env, "production" 

到我的deploy.rb,但这看起来像硬编码给我,我相信有一个更好的解决方案。

编辑:根据此拉取请求 ,它现在已在capistrano-rails版本1.1.0中修复。

根据这个Github问题 ,另一个修复是编辑你的Capfile 。 注释掉这两行

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

把这一行放进去

需要'capistrano / rails'

这将正确设置您的RAILS_ENV变量。

在导轨4上使用Cap 3和capistrano_rails我得到了同样的错误; 在我正在部署的环境文件中,我设置了

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

Doc在这里: https : //github.com/capistrano/rails

似乎是capistrano-rails中的一个错误。

有一个任务(rails.rake)可以从rails_env或stage设置环境:

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

但是之前没有调用此任务,即资产:预编译。 所以这:

 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 

失败,因为如果未明确设置rails_env为nil。

如果我有时间深入挖掘,我会提交错误报告。

根据Marc的答案肯定是正确的,

您可以解决此问题,直到上游修复为止,将其添加到“namespace:deploy”块中的config / deploy.rb:

  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" 

这将强制在资产:precompile被调用之前加载env和配置RAILS_ENV。

如果您正在使用乘客,则需要添加

 rails_env production; 

在web服务器(例如:nginx).conf中,您为passenger_rubypassenger_root指定了值。

如果添加文件会发生什么:

 deploy/production.rb 

有了这条线:

 set :stage, :production