多级capistrano:db:migrate在生产时正常工作,但在暂存时不工作

时间:2012-12-12 21:23:19

标签: ruby-on-rails ruby capistrano

我已经设置了capistrano来部署到舞台和制作。老实说,我对capistrano不太熟悉。我通过使用标准capistrano(不是多主机)来做到这一点。我传递了一个变量,如:

cap production deploy
cap staging deploy

但我的db:migrate无效。

使用cat staging部署: 我明白了:

  * executing "cd /data/sites/staging.domain.com/apps/d-rails/releases/20121212203353 && bundle exec rake RAILS_ENV=production  db:migrate"

并希望(只是次生产 - >分期):

* executing "cd /data/sites/staging.domain.com/apps/d-rails/releases/20121212203353 && bundle exec rake RAILS_ENV=staging  db:migrate"

我该如何设置?或者我应该先看一下修复什么?

在我的deploy.rb中,我有:

task :production do
  set :deploy_to, "/data/sites/domain.com/apps/#{application}"
end

task :staging do
  set :deploy_to, "/data/sites/staging.domain.com/apps/#{application}"
  after 'deploy:update_code' do
    run "cd #{release_path}; RAILS_ENV=staging bundle exec rake assets:precompile --trace"
  end
end
事先提前

1 个答案:

答案 0 :(得分:4)

我认为使用capistrano的多阶段功能会更容易。以下是我的生产和暂存部署设置:

<强>配置/ deploy.rb

require 'capistrano/ext/multistage'
require 'bundler/capistrano'

set :application, "yourappname"
set :repository,  "git@yourhost.com:yourrepo.git"
set :stages, %w(production staging)
set :default_stage, "staging" # running "cap deploy" deploys to staging, "cap production deploy" deploys to production
set :user, "deploy" # the ssh user which does the deployment on the server
set :use_sudo, false
set :scm, :git

set :default_environment, {
  'PATH' => "/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/versions/1.9.3-p327/bin:$PATH"
}
after "deploy:update_code", "deploy:migrate"

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

如果您必须为部署添加一些其他修补程序,则需要set :default_environment(因为,当capistrano登录到服务器时,不包括正常.bashrc.bash_profile < / p>

<强>配置/部署/ production.rb

set :rails_env, "production"
set :deploy_to, "/var/www/your_production_folder"

role :web, "example.com"                          # Your HTTP server, Apache/etc
role :app, "example.com"                          # This may be the same as your `Web` server
role :db,  "example.com", :primary => true # This is where Rails migrations will run

<强>配置/部署/ staging.rb

set :rails_env, "staging"
set :deploy_to, "/var/www/your_staging_folder"

role :web, "example.com"                          # Your HTTP server, Apache/etc
role :app, "example.com"                          # This may be the same as your `Web` server
role :db,  "example.com", :primary => true # This is where Rails migrations will run

确保在VirtualHost配置中包含RailsEnv变量。如果您使用的是Apache,那么它将如下所示:

<VirtualHost *:80>
  ServerName staging.example.com
  ServerAlias www.staging.example.com
  # !!! Be sure to point DocumentRoot to 'public'!
  DocumentRoot /var/www/your_staging_folder/current/public
  <Directory /var/www/your_staging_folder/current/public>
    # This relaxes Apache security settings.
    AllowOverride all
    # MultiViews must be turned off.
    Options -MultiViews
    #AuthName "Staging Server"
    #AuthType Basic
    #AuthUserFile /var/staging.htpasswd
    #require valid-user
  </Directory>
  RailsEnv staging
</VirtualHost>

如果您想要密码保护您的暂存环境,则使用未注释的AuthNameAuthType。完成配置后,使用cap deploy:setup测试部署,这将设置文件夹结构。 cap deploy:cold会将应用程序的所有文件复制到目录中。 cap deploy:migrate迁移您的数据库。但你也可以只做cap deploy

另一件事是,你必须在rails应用程序中设置一个staging env。为此,请将config/environments/production.rb(或您喜欢的开发.rb)复制到staging.rb并根据需要调整配置。

我希望我没有忘记任何事情;)如果您有任何进一步的问题,请告诉我