0

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

cap production deploy
cap staging deploy

但我db:migrate的工作不正确。

使用 cat staging deploy:我明白了:

  * 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

提前谢谢

4

1 回答 1

4

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

配置/部署.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如果您必须为您的部署包含一些额外的路径,则需要 (因为,正常或.bashrc.bash_profilecapistrano 登录服务器时不包含)

配置/部署/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>

如果您想用密码保护您的登台环境AuthName,则使用未注释的, 。AuthType完成配置这些东西后,使用 测试您的部署cap deploy:setup,这将设置文件夹结构。Acap deploy:cold会将您的应用程序的所有文件复制到该目录。Acap deploy:migrate迁移您的数据库。但你也只能做一个cap deploy.

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

我希望我没有忘记任何事情;)如果您还有其他问题,请告诉我

于 2012-12-12T21:55:09.497 回答