deploy.rb 2.27 KB
require "awesome_print"
# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'nuozheng'
set :repo_url, 'git@git.ebookchain.net:sdtailor/nuozheng.git'
set :branch, "master"
set :deploy_to, '/home/web/nuozheng'
set :scm, :git
set :pty, false

set :keep_releases, 3
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn_nuozheng.pid"

namespace :deploy do

  task :start do
    on roles(:web) do
      within current_path do
        with rails_env: 'production' do
          execute :bundle, "exec unicorn_rails -c #{fetch(:unicorn_config)} -D"
        end
      end
    end
  end
 
  task :stop do
    on roles(:web) do
      execute "if [ -f #{fetch(:unicorn_pid)} ]; then kill -QUIT `cat #{fetch(:unicorn_pid)}`; fi"
    end
  end
  
  task :restart do
    on roles(:web) do
      # within current_path do
        # 用USR2信号来实现无缝部署重启
        execute "if [ -f #{fetch(:unicorn_pid)} ]; then kill -s USR2 `cat #{fetch(:unicorn_pid)}`; fi"
      # end
    end
  end

  task :mkdir_shared do
    on roles(:web) do
      execute "mkdir -p #{shared_path}/uploads"
      execute "mkdir -p #{shared_path}/log"
      execute "mkdir -p #{shared_path}/assets"
      execute "mkdir -p #{shared_path}/tmp/pids"
      execute "mkdir -p #{shared_path}/tmp/cache"
    end
  end

  task :link_shared do
    on roles(:web) do
      execute "ln -sf #{shared_path}/log #{current_path}/log"
      execute "ln -sf #{shared_path}/assets #{current_path}/public/assets"
      execute "ln -sf #{shared_path}/tmp #{current_path}/tmp"
      execute "ln -sf #{shared_path}/uploads #{current_path}/public/uploads"
    end
  end

  task :db_migrate do
    on roles(:web) do
      within current_path do
        with rails_env: 'production' do
          execute :bundle, :exec, :rake, 'db:migrate'
        end
      end
    end
  end

  task :bundle_install do
    on roles(:web) do
      within current_path do
        with rails_env: 'production' do
          execute :bundle, 'install'
        end
      end
    end
  end

  before :starting,  :mkdir_shared
  after :publishing, :link_shared
  after :publishing, :bundle_install
  after :publishing, :db_migrate
  after :publishing, :restart # 第一次部署会有找不到unicron.pid的bug

end