http://blog.csdn.net/qzier_go/article/details/35971091
https://github.com/capistrano/capistrano
https://ruby-china.org/topics/18616
http://dl528888.blog.51cto.com/2382721/1270670
http://www.cnblogs.com/iosdev/p/3349405.html
http://bbs.apiusage.com/read/297
1 安装
gem install capistrano // For mutiple stages gem install capistrano-ext |
2 准备
capify . |
这个命令会创建Capfile,和 config/deploy.rb 文件。Capfile会帮助加在一些合适的库文件和配置。
配置deploy.rb
set :application, "applciation_name" //git set :scm, :git set :repository, "git@www.xxx.com:/repository.git" set :scm_passphrase, "" //sub version set :scm, :subversion // user name set :user, "server-user-name" |
多stage需要包含文件
require 'capistrano/ext/multistage' |
然后
set :stages, [ "staging" , "production" ] set :default_stage, "staging" |
然后在config目录下创建一个文件夹deploy 然后创建两个文件 production.rb staging.rb
production.rb
server "xxx.com" , :app, :web, :db, :primary => true set :deploy_to, "/var/www/application_name" |
staging.rb
server "xxx.com" , :app, :web, :db, :primary => true set :deploy_to, "/var/www/application_staging" |
3 一些设置
cap deploy:setup |
这条命令SSH到服务器,创建deploy_to的目录。
errors:
[xxx.com] executing command *** [err :: xxx.co] sudo *** [err :: xxx.co] : *** [err :: xxx.co] sorry, you must have a tty to run sudo *** [err :: xxx.co] command finished in 718ms failed: "sh -c 'sudo -p '\''sudo password: '\'' mkdir -p /var/www/TTQuan_staging /var/www/xxx_staging/releases /var/www/xxx_staging/shared /var/www/xxx_staging/shared/system /var/www/TTQuan_staging/shared/log /var/www/xxx_staging/shared/pids'" on xxx.co |
要添加
default_run_options[:pty] = true |
确信你的设置没有问题
cap deploy:check |
You appear to have all necessary dependencies installed
最后部署
cap deploy |
cap production deploy |
5 Tips & Tricks
1) Capistrano 第一次部署都会clone/export respository,然后之后每次git pull/svn up就会替代clone/export。如果你经常deploy 那这样会加速部署
set :deploy_via, :remote_cache |
2)创建tasks
namespace :deploy do task :restart, :roles => :web do run "touch #{ current_path }/tmp/restart.txt" end task :restart_daemons, :roles => :app do sudo "monit restart all -g daemons" end end |
Capistrano 不仅仅是通过ssh复制文件。完成复制文件之后你还可以配置一些事件和命令,例如通过自定义脚本重启服务器。Capistrano叫这些为“tasks”。
这里例子很简单。restart就是部署完成之后运行的。 “touch tmp/restart.txt“ 是用passenger时候使用的,但是如果你使用unicorn就换成unicron的就可以了。
下面的task 不会自动运行
after "deploy" , "deploy:restart_daemons" |
3)关联环境的branches
production.rb
set :branch, 'production' |
staging.rb
set :branch, 'staging' |
cap deploy 默认是staging
cap production deploy