Skip to content

Instantly share code, notes, and snippets.

@jancel
Created March 31, 2014 14:43
Show Gist options
  • Save jancel/9893965 to your computer and use it in GitHub Desktop.
Save jancel/9893965 to your computer and use it in GitHub Desktop.
Example deploy.rb for Capistrano
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
run cmd
set :rakefile, nil if exists?(:rakefile)
end
require 'rvm/capistrano'
# Bundler
set :bundle_roles, [:app]
require 'bundler/capistrano'
load 'deploy/assets'
set :git_name, "jancel"
set :user, 'deployment'
set :application, "listr"
default_run_options[:pty] = true
set :repository, "[email protected]:#{git_name}/#{application}.git"
set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :deploy_to, "/home/#{user}/#{application}"
set :git_shallow_clone, 1
set :keep_releases, 5
set :branch do
default_tag = `git tag`.split("\n").last
tag = Capistrano::CLI.ui.ask "Tag to deploy (make sure to push the tag first): [#{default_tag}] "
tag.empty? ? default_tag : tag
end
set :use_sudo, false
server '10.10.10.10', :web, :app, :db, :primary => true
ssh_options[:keys] = [File.join(ENV['HOME'], 'id_rsa')]
ssh_options[:port] = 22
ssh_options[:forward_agent] = true
ssh_options[:paranoid] = false
namespace :custom do
desc "Add config symlinks"
task :links, :roles => :app do
#["database", "application","features"].each do | cfg |
cfg = "database"
run <<-CMD
ln -s #{shared_path}/config/#{cfg}.yml #{release_path}/config/#{cfg}.yml
CMD
#end
end
desc "Run db seeding"
task :seed, :roles => :app do
run_remote_rake "db:seed"
end
end
set :unicorn_pid do
"#{shared_path}/pids/unicorn.pid"
end
after "bundle:install", "custom:links"
#after "custom:links", "deploy:assets:precompile"
#before "deploy:assets:precompile", "bundle:install"
#after "bundle:install", "custom:links"
#before "deploy:migrate", "custom:seed"
#after "deploy:migrate", "custom:seed"
# remove old releases
after "deploy", "deploy:cleanup"
namespace :deploy do
task :start do
top.unicorn.start
#top.resque.start
end
task :stop do
top.unicorn.stop
#top.resque.stop
end
task :restart do
top.unicorn.reload
#top.resque.restart
end
end
namespace :unicorn do
desc "start unicorn"
task :start, :roles => :app do
run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -E #{rails_env} -D"
end
desc "stop unicorn"
task :stop, :roles => :app do
run "#{try_sudo} kill -s QUIT `cat #{unicorn_pid}`"
end
desc "restart unicorn"
task :restart, :roles => :app do
top.unicorn.stop
top.unicorn.start
end
desc "reload unicorn (gracefully restart workers)"
task :reload, :roles => :app do
run "#{try_sudo} kill -s USR2 `cat #{unicorn_pid}`"
end
desc "reconfigure unicorn (reload config and gracefully restart workers)"
task :reconfigure, :roles => :app do
run "#{try_sudo} kill -s HUP `cat #{unicorn_pid}`"
end
end
namespace :resque do
desc "start workers"
task :start, :roles => :app do
run_remote_rake "resque:start_workers"
end
desc "stop worlers"
task :stop, :roles => :app do
run_remote_rake "resque:stop_workers"
end
desc "restart workers"
task :restart, :rolse => :app do
run_remote_rake "resque:restart_workers"
end
end
#set :application, "set your application name here"
#set :repository, "set your repository location here"
# 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, "your web-server here" # Your HTTP server, Apache/etc
#role :app, "your app-server here" # This may be the same as your `Web` server
#role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"
# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
# namespace :deploy do
# task :start do ; end
# task :stop do ; end
# task :restart, :roles => :app, :except => { :no_release => true } do
# run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
# end
# end
source 'https://rubygems.org'
gem 'rails', "3.2.10"
# omitted
group :stage, :production do
gem 'pg'
gem 'unicorn'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platforms => :ruby
gem 'libv8'
gem 'uglifier', '>= 1.0.3'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
group :test do
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'simplecov', :require => false
gem 'launchy', :require => false
gem 'mocha'
# gem 'email_spec'
end
group :development, :test do
gem 'debugger'
gem 'rspec-rails'
gem 'capybara'
gem 'sqlite3'
gem 'vcr'
gem 'fakeweb'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
end
group :development do
gem 'capistrano'
gem 'rvm-capistrano'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment