Skip to content

Instantly share code, notes, and snippets.

@zukowski
Created November 24, 2014 23:14
Show Gist options
  • Save zukowski/a37e34ee290847cc1f19 to your computer and use it in GitHub Desktop.
Save zukowski/a37e34ee290847cc1f19 to your computer and use it in GitHub Desktop.
Example capistrano deploy script
set :stages, %w(production)
require 'capistrano/ext/multistage'
require 'bundler/capistrano'
require 'rvm/capistrano'
require './config/boot'
ssh_options[:username] = 'deployer'
ssh_options[:forward_agent] = true
set :application, 'my-app'
set :repository, "[email protected]:Organization/#{application}.git"
set :scm, :git
set(:deploy_to) {"/var/www/#{application}/#{rails_env}"}
set :rvm_type, :user
set :rvm_ruby_string, "2.1.3@#{application}"
set :use_sudo, false
set :shared_symlinks, %w(config/database.yml config/unicorn.rb config/secrets.yml)
set :tasks_for_rake, %w(db:migrate)
before 'deploy:assets:precompile', 'deploy:create_shared_symlinks'
after 'deploy:assets:precompile', 'deploy:db:migrate'
after 'deploy:db:migrate', 'nginx:config'
after 'nginx:config', 'nginx:reload'
after 'deploy', 'deploy:cleanup'
namespace :deploy do
task :start do
run "cd #{current_path} && bundle exec unicorn -c #{shared_path}/config/unicorn.rb -E #{rails_env} -D"
end
task :stop do
run "kill `cat #{shared_path}/pids/unicorn.pid`; true"
end
task :restart do
stop
start
end
task :create_shared_symlinks, :except => {:no_release => true} do
run(shared_symlinks.map {|link| "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"}.join(' && '))
end
namespace :db do
task :backup, :except => {:no_release => true} do
run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rake db:backup"
end
task :migrate, :except => {:no_release => true} do
backup
run "cd #{release_path} && RAILS_ENV=#{rails_env} bundle exec rake db:migrate"
end
task :seed, :except => {:no_release => true} do
run "cd #{release_path} && RAILS_ENV=#{rails_env} bundle exec rake db:seed"
end
end
end
namespace :nginx do
task :config do
run "sudo rm -f /etc/nginx/sites-enabled/#{application}_#{rails_env} && sudo ln -nfs #{current_path}/config/nginx_#{rails_env}.conf /etc/nginx/sites-enabled/#{application}_#{rails_env}"
end
task :reload do
run "sudo /etc/init.d/nginx reload"
end
end
require './config/boot'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment