实例 IP:192.168.1.100 开发服务器,capistrano 发布服务器 svn地址:svn://192.168.1.100/svnweb svn发布目录:/app/dev_public IP:192.168.1.200 web服务器
web目录: /opt/webroot/
capistrano 发布原理 使用svn用户将svn项目 svn://192.168.1.100/svnweb checkout 到/tmp/20130108***下通过sftp将/tmp/20130108***文件打为tgz,并上传到web服务器tmp/20130108***.tgz,并将 /tmp/20130108***.tgz解压到web服务器/opt/webroot/目录。
安装 capistrano 基于Ruby管理工具( RubyGames )安装,先安装Ruby,再安装RubyGames,最后安装Capistrano Ruby下载 RubyGames下载 Ruby安装# tar jxf ruby-1.9.2-p0.tar.bz2 # cd ruby-1.9.2-p0 # ./configure # make && make install RubyGames安装
# tar zxv rubygems-1.8.9.tgz # cd rubygems-1.8.9
# /usr/local/bin/ruby setup.rb
Capistrano安装
# gem install Capistrano
配置
# cd /home/dev_public
# capify . (注意后面的.号)
capify .执行后会在目录里Capfile文件和一个config文件夹,config文件夹里deploy.rb配置文件。
/home/dev_public
|-- Capfile`-- config `-- deploy.rb
修改deploy.rb配置文件,内容如下:
# vi /app/dev_public/config/deploy.rb
set :application, "dev_public01"set :repository, "svn://192.168.1.100/svnweb"
set :scm, :subversionset :scm_username, 'dev_public'set :scm_password, 'dev_public'set :checkout, "export"
role :web, "192.168.1.200"#role :db, "192.168.1.201"
set :deploy_to, "/opt/webroot/"set :deploy_via, :copy
set :user, "root"set :runner, "devpass"set :password, "devpass"
set :port, 2222set :use_sudo, true
set :keep_releases, 3namespace :deploy do task :restart, :roles => :web do run "sudo chown www:www -R /opt/webroot/" endend
配置说明:
set :application 定义项目名称
set :repository svn地址
set :scm 定义svn名称
set :scm_username svn用户名
set :scm_password svn密码
set :checkout svn执行方式
role :web 定义web服务器地址,可定义多个
set :deploy_to 定义web服务器目录
set :deploy_via 定义部署文件,copy
set :user web服务器登陆用户名,也可使用ssh密钥登陆set :runner web服务器运行用户名
set :password web服务器密码
set :port web服务器ssh端口
set :use_sudo 使用sudo
set :keep_releases 存储版本个数,执行cap deploy:cleanup清理版本时,保留的版本个数
发布版本时,在web服务器上执行相关命令
namespace :deploy do task :restart, :roles => :web do run "sudo chown www:www -R /opt/webroot/" endend
注意 : 将web服务器的/etc/sudoers文件中的 Defaults requiretty 注释掉,否则在执行cap deploy:setup时会报以下错误。
sudo: sorry, you must have a tty to run sudo
发布
# cd /app/dev_public
# cap deploy:setup #在web服务器上建相应目录,为发布代码做准备
/opt/webroot / |-- current |-- releases `-- shared |-- log |-- pids `-- system # cap deploy:check #检查web服务器上文件夹权限 # cap deploy #代码发布
cap deploy:rollback #可以快速回滚到上一版本
cap deploy:cleanup #清理版本,且保留版本数量与deploy.rb配置文件set :keep_releases参数有关。
# cap -vT 可查看相关参数,如下:
cap deploy # Deploys your project. cap deploy:check # Test deployment dependencies. cap deploy:cleanup # Clean up old releases. cap deploy:cold # Deploys and starts a `cold' application. cap deploy:create_symlink # Updates the symlink to the most recently deplo... cap deploy:finalize_update # [internal] Touches up the released code. cap deploy:migrate # Run the migrate rake task. cap deploy:migrations # Deploy and run pending migrations. cap deploy:pending # Displays the commits since your last deploy. cap deploy:pending:diff # Displays the `diff' since your last deploy. cap deploy:restart # cap deploy:rollback # Rolls back to a previous version and restarts. cap deploy:rollback:cleanup # [internal] Removes the most recently deployed ... cap deploy:rollback:code # Rolls back to the previously deployed version. cap deploy:rollback:revision # [internal] Points the current symlink at the p... cap deploy:setup # Prepares one or more servers for deployment. cap deploy:start # Blank task exists as a hook into which to inst... cap deploy:stop # Blank task exists as a hook into which to inst... cap deploy:symlink # Deprecated API. cap deploy:update # Copies your project and updates the symlink. cap deploy:update_code # Copies your project to the remote servers. cap deploy:upload # Copy files to the currently deployed version. cap invoke # Invoke a single command on the remote servers. cap shell # Begin an interactive Capistrano session