capistrano 是一套強大的佈署工具, 今天提供小弟的設定檔內容給大家參考
rvm 和 bundler
config/deploy.rb| 1
2
3
 | $:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "rvm/capistrano" # Load RVM's capistrano plugin.
require 'bundler/capistrano'
 | 
 
capistrano color, 讓佈署過程中的訊息內容上色
Gemfile 必須先加上 gem ‘capistrano_colors’
config/deploy.rb| 1
2
3
4
5
 | begin
  require 'capistrano_colors'
rescue LoadError
  puts "`gem install capistrano_colors` to get output more userfriendly."
end
 | 
 
cron 設定, 透過 whenever gem 在 deploy 時, 更新 cron
Gemfile 要有 gem ‘whenever’
config/deploy.rb| 1
 | require "whenever/capistrano"
 | 
 
multistages 設定, 讓 server 端的設定和佈署工作各自獨立管理, 同時資源佈署多個 server
config/deploy.rb| 1
2
3
 | require 'capistrano/ext/multistage'
set :stages,        %w(staging production)
set :default_stage, "staging"
 | 
 
基本設定, 包含 git
config/deploy.rb| 1
2
3
4
5
6
 | set :application, "foo"
set :repository,  "git@github.com:marsz/foo.git"
set :scm, :git
set :use_sudo, false
 | 
 
設定 deploy:restart, capistrano 預設是空的, 所以重起 rack 的部份一定要自己寫, 以下範例以 passenger + apache 的重啟方式做為參考
config/deploy.rb| 1
2
3
4
5
6
7
8
 | # namespace :deploy 內
namespace :deploy do
  # .....
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "touch #{current_path}/tmp/restart.txt"
  end
  # .....
end
 | 
 
設定 symlink_shared, 有一些沒進 version control 的 config 檔案, 要在佈署時, link 過去, 例如 config/database.yml, 以下僅供參考, 實際 link 內容須自己寫
config/deploy.rb| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 | # namespace :deploy 內
namespace :deploy
  # ....
  task :symlink_shared, :roles => [:app] do
    config_files = [:database, :redis]
    symlink_hash = {}
    config_files.each do |fname|
      from = "#{shared_path}/config/#{fname}.yml"
      to = "#{release_path}/config/#{fname}.yml"
      run "ln -s #{from} #{to}"
    end
  end
  # ....
end
# 把 symlink_shared 內容掛進去 deploy 中
before "bundle:install", "deploy:symlink_shared"
 | 
 
tail log, 要線上 debug 時可用
config/deploy.rb| 1
2
3
4
5
 | task :tail_log, :roles => :app do
  run "tail -f -n 100 #{shared_path}/log/#{rails_env}.log"
end
# 執行 cap production tail_log 即可
 | 
 
使用了 multistage 後, 各個 server 的設定檔必須放在 config/deploy/ 下, 檔名對應 stage 名稱
例如 config/deploy/production.rb
以下為 stage 設定檔內容
預設 rails env
config/deploy/production.rb| 1
 | set :rails_env, "production"
 | 
 
github 分支
config/deploy/production.rb
ssh 登入相關, 建議多利用  SSH 免密碼登入 
config/deploy/production.rb| 1
2
3
 | set :user, 'marsz'
set :domain, 'xxx.com'
server "#{domain}", :web, :app, :db, :primary => true
 | 
 
佈署的 dir path
config/deploy/production.rb| 1
 | set :deploy_to, "/path/to/app"
 | 
 
佈署指令
config/deploy/production.rb| 1
2
3
 | cap production setup # 第一次佈署環境建立
cap production deploy # 進行佈署
cap production deploy:migrations # 進行佈署並且跑 migration
 | 
 
若想要在 cap deploy 時也執行 rake assets:precompile
可以參考 這篇