部署 rvm bundler capistrano rails 的部署策略问题

AReverie · 2013年01月28日 · 最后由 huobazi 回复于 2013年01月29日 · 3060 次阅读

(标题那个 rails 只是顺带的)

大家好,最近开始接触 capistrano,加上才开始熟悉 ruby 的生态环境,对 gem bundler rvm 等都有些兴趣。

这几天有个玩具性质的项目需要上线,于是顺便就折腾一下 capistrano 等,大家帮某看看是不是有问题或者比较好的最佳实践 :D

项目使用 rails 写,服务器运行选择 unicorn

环境

服务器上部署使用 rvm 来管理 ruby 与 gemsets,自动部署使用 capistrano

在 recipe/Capfile 里加了bundler/capistranorvm/capistrano

gemsets 没有单独创建,就用了 ruby-1.9.3-p374 自己的,这个会有问题吗?

发布代码

开发完成 push 进 git 后 cap deploy

用了bundler/capistrano后,每次deploy:finalize_update都会运行bundle:install,默认的 path 在shared/bundle下面,但因为 release 目录变了还需要去扫一边 Gemfile,好像会再重新安装一下 gem(这个某没仔细确认)

现在的方法是某第一次项目代码上去在shared_children里加入 vendor,让项目下 symlink,bundle install后,package一下这样就不用重复下 gem 了。

重启 unicorn

有一个 capistrano-unicorn 的 gem 没用,某自己的方案是使用了rvm/capistrano,这时指定 ruby 版本后已经使用对应 gemset 里的 unicorn 命令(写了个测试的 task 去看了一下 unicorn 的路径),然后直接把config.ruunicorn.conf重新做 symlink 到 shared 下面去 ,然后该干吗干吗了(倒是要把 rails 默认的 config.ru 里路径 readlink 一下)

最后给一下某现在的 Capfile 与 deploy.rb

大家看看是不是太麻烦了?一般性策略是什么样的

谢谢

Capfile

load 'deploy'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
require "rubygems"
require "rvm/capistrano"
require "bundler/capistrano"
load 'config/deploy'

deploy.rb

set :user, "foobar"
set :application, "suika"
set :repository, "ssh://path/to/suika.git"
ssh_options[:forward_agent] = true
ssh_options[:compression] = false
set :scm, :git
set :deploy_to, "/path/to/suika"
set :deploy_via, "copy"
set :normalize_asset_timestamps, false
set :use_sudo, false
set :shared_children, %w(vendor public/system log tmp/pids)

# rvm
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")

role :app, "server"
server "server", :app

namespace :deploy do
  task :compile_assets do
    run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
  end

  task :symlink_database do
    run "ln -s #{shared_path}/production.sqlite3 #{release_path}/db/"
  end

  task :copy_unicorn_conf do
    run "ln -snf #{release_path}/unicorn.conf.rb #{shared_path}/unicorn.conf.rb"
    run "ln -snf #{release_path}/config.ru #{shared_path}/config.ru"
  end

  task :unicorn_restart do
    run "cd #{shared_path}; kill -HUP $(cat pids/unicorn.pid)"
  end

  task :unicorn_start do
    run "cd #{shared_path}; unicorn -c unicorn.conf.rb -D -p 3000"
  end
end

after 'deploy:update_code', 'deploy:compile_assets'
after 'deploy:create_symlink', 'deploy:symlink_database'
after 'deploy:symlink_database', 'deploy:copy_unicorn_conf'

看得头晕啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号