Ruby on Rails3 Capistrano Rvm (Revised)

以前在rvm下没有rvm-capistrano的时候,自己去写shell还是难过!

添加到Gemfile

Gemfile
1
2
3
4
group :development do
  gem 'capistrano', '2.9.0', :require => false
  gem 'rvm-capistrano', :require => false
end
1
bundle install
1
2
3
4
$ capify .
[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!
deploy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require "rvm/capistrano"
#https://rvm.io/integration/capistrano/

default_run_options[:pty] = true
set :rvm_ruby_string, 'ruby-1.9.3-p362'
set :rvm_type, :user

set :application, "new_xxx"
set :repository,  "git@github.com:xxx/new_xxx.git"
set :branch, "master"
set :scm, :git
set :user, "john"
set :use_sudo, false

set :deploy_to, "/home/john/deploy/#{application}"
set :runner, "ruby"
# set :deploy_via, :remote_cache
set :deploy_via, :remote_cache
#set :git_shallow_clone, 1

# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

role :web, "staging.xxx.com"                          # Your HTTP server, Apache/etc
role :app, "staging.xxx.com"                          # This may be the same as your `Web` server
role :db,  "staging.xxx.com", :primary => true # This is where Rails migrations will run
role :db,  "staging.xxx.com"


  namespace :deploy do
      task :start, :roles => :app do
          run ["cd #{deploy_to}/current/","RAILS_ENV=production bundle exec thin start -C config/thin.yml"].join(' && ')
      end

      task :stop, :roles => :app do
          run ["cd #{deploy_to}/current/","RAILS_ENV=production bundle exec thin stop -C config/thin.yml"].join(' && ')
      end

      desc "Restart Application"
      task :restart, :roles => :app do
          run ["cd #{deploy_to}/current/","RAILS_ENV=production bundle exec thin restart -C config/thin.yml"].join(' && ')
      end
  end


desc "tail log"
task :log, :roles => :app do
  stream "tail -n100 -f #{deploy_to}/current/log/#{rails_env.downcase}.log"
end


task :link_shared_files, :roles => :web do
  run "ln -sf #{deploy_to}/shared/Gemfile.lock #{deploy_to}/current/Gemfile.lock"
  run "ln -sf #{deploy_to}/shared/cache #{deploy_to}/current/vendor/cache"
  run "ln -sf #{deploy_to}/shared/mnt/uploads #{deploy_to}/current/public/uploads"
end


desc 'compile_assets production'
task :compile_assets, :roles => :web do
  run "cd #{deploy_to}/current/; RAILS_ENV=production bundle exec rake assets:precompile"
end    

desc 'init bundle env'
task :init_bundle, :roles => :web do
  run "cd #{deploy_to}/current/; bundle install --without development test"
end    
#
after "deploy:finalize_update","deploy:symlink",:link_shared_files, :init_bundle, :compile_assets

初始化

1
cap deploy:setup

布署

1
2
3
4
5
6
7
8
9
$ cap deploy
  * 2013-01-22 14:34:59 executing `deploy'
  * 2013-01-22 14:34:59 executing `deploy:update'
 ** transaction: start
  * 2013-01-22 14:34:59 executing `deploy:update_code'
    executing locally: "git ls-remote git@github.com:xxx/xxx.git master"
    command finished in 6756ms
  * executing "git clone -q --depth 1 git@github.com:xxx/xxx.git /home/john/deploy/xxx/releases/20130122063506 && cd /home/john/deploy/xxx/releases/20130122063506 && git checkout -q -b deploy c7d9acbaaef3a9ff09ec3f751969a00544f90230 && (echo c7d9acbaaef3a9ff09ec3f751969a00544f90230 > /home/john/deploy/xxx/releases/20130122063506/REVISION)"
    servers: ["staging.xxxx.com"]
1
2
3
4
cap deploy:update_code#更新代码
cap deploy:start
cap deploy:stop
cap log #本地就是可以看服务器的production.log

ref: https://rvm.io/integration/capistrano/

原文地址:http://dlj.bz/3P1hcz

Comments