Skip to content

Instantly share code, notes, and snippets.

@vincent178
Last active December 21, 2015 11:09
Show Gist options
  • Save vincent178/6297086 to your computer and use it in GitHub Desktop.
Save vincent178/6297086 to your computer and use it in GitHub Desktop.
cap code snippets
# =========================
# For Bundler
# Cap will run "bundle --deploy" on the production server
# every time when you deploy
# ==========================
require 'bundler/capistrano'
set :application, "wechat_bot"
# ==========================
# Single server
# ==========================
server "192.81.133.63", :app, :web, :db, :primary => true
# ==========================
# Set app path
# ==========================
set :application, "weixin"
set :user, "deploy"
set :deploy_to, "/home/#{user}/app/#{application}"
# ==========================
# Use rbenv in cap
# ==========================
set :default_environment, {
'RBENV_ROOT' => "/home/#{user}/.rbenv/bin/rbenv",
'PATH' => "/home/#{user}/.rbenv/shims:/home/#{user}/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:$PATH"
}
# ==========================
# Git configuration
# ==========================
set :scm, :git
set :repository, "git@github.com:vincent178/wechat.git"
set :branch, "master"
# ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]
# ssh_options[:config] = false
# use ssh key to do a password-less login
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
# Reuse the repo instead of brand new git clone each time
set :depoly_via, :remote_cache
set :use_sudo, false
# ==========================
# For Migrations
# "cap deploy:migrate" only run migrations
# "cap deploy:migrations" run deploy and migrations
# it won't hurt after every deploy
# ==========================
# after 'deploy:update_code', 'deploy:migrate'
set :keep_release, 5
after "deploy:restart", "deploy:cleanup"
# ==========================
# Unicorn settings
# ==========================
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
namespace :nginx do
task :link, except: { no_release: true } do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/nginx.conf "
end
end
namespace :deploy do
task :start, :except => { no_release: true } do
run "cd #{current_path} && RAILS_ENV=production bundle exec unicorn_rails -c #{unicorn_config} -D"
end
task :stop, except: { no_release: true } do
run "if [ -f #{unicorn_pid} ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
end
task :restart, except: { no_release: true } do
run "if [ -f #{unicorn_pid} ]; then kill -s USR2 `cat #{unicorn_pid}`; fi"
end
task :clear_log, except: { no_release: true } do
run "cd #{shared_path}/log && echo '' > production.log"
end
task :setup_config do
run "mkdir -p #{shared_path}/config"
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/nginx.conf "
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}"
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push ` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment