capistrano 代碼發佈簡單使用

實例
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下載                   http://ishare.iask.sina.com.cn/download/explain.php?fileid=9554760
RubyGames下載          http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.8.9.tgz  
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, :subversion
set :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, 2222
set :use_sudo, true
set :keep_releases, 3
namespace :deploy do
     task :restart, :roles => :web do
         run "sudo chown www:www -R /opt/webroot/"
     end
end
 
配置說明:
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/"
     end
end
 
注意將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
相關文章
相關標籤/搜索