我认为使用 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_profile
capistrano 登录服务器时不包含)
配置/部署/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
并根据需要调整配置。
我希望我没有忘记任何事情;)如果您还有其他问题,请告诉我