【问题标题】:Multistaging capistrano: db:migrate working correctly on production but not on staging多阶段 capistrano:db:migrate 在生产中正常工作,但在暂存时无法正常工作
【发布时间】:2012-12-12 21:23:18
【问题描述】:

我已设置 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

提前谢谢

【问题讨论】:

  • 您使用 capistrano 的多阶段功能吗?为什么暂存和生产部署到同一个文件夹?
  • 不使用多阶段功能;不确定您的部署?,一个去 domain.com,另一个去 staging.domain.com
  • 我已经在下面给你写了答案
  • 如果我的回答解决了您的问题,请将其标记为已接受;)

标签: ruby-on-rails ruby capistrano


【解决方案1】:

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

config/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

config/deploy/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

config/deploy/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 应用程序中设置暂存环境。为此,请将 config/environments/production.rb(或你喜欢的 development.rb)复制到 staging.rb 并根据需要调整配置。

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

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 2018-02-09
    • 1970-01-01
    • 2016-09-01
    • 2012-07-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多