Skip to content

Instantly share code, notes, and snippets.

@robfeldmann
Created June 2, 2013 20:32
Show Gist options
  • Save robfeldmann/5694858 to your computer and use it in GitHub Desktop.
Save robfeldmann/5694858 to your computer and use it in GitHub Desktop.
A Capistrano deployment recipe that works for my Ubuntu, Apache, Passenger, RVM, Bundler, Ruby on Rails setup (Linode). It's based on this gist https://gist.github.com/meskyanichi/157958. I just added RVM and Bundler support and remove a bunch of stuff I'm not currently using.
# =================================== #
# RVM/CAPISTRANO/BUNDLER SETUP #
# =================================== #
set :rvm_ruby_string, :local # use the same ruby as used locally for deployment
set :rvm_autolibs_flag, "install-packages" # more info: rvm help autolibs
set :rvm_install_with_sudo, true #:use_sudo is set to 'false' but sudo is needed to install requirements with autolibs
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset
require "rvm/capistrano"
require 'bundler/capistrano'
# =================================== #
# START CONFIGURATION #
# =================================== #
# This configuration is *essential*
set :site, "example.com" # main domain, also used for ssh
set :application, "app_name.example.com" # the url of the application
set :user, "deploy"
set :ssh_options, { :forward_agent => true, :port => 2222 } # replace 2222 with actual port number
# This configuration is *conventional*
set :deploy_to, "/home/#{user}/www/#{application}"
set :deploy_via, :copy
set :repository_path, "/home/#{user}/git/#{application}.git"
set :repository, "ssh://#{site}/#{repository_path}"
# The following configuration *optional*
set :rails_env, "production"
set :keep_releases, 5
set :scm, "git"
set :branch, "master"
set :use_sudo, false
role :web, site
role :app, site
role :db, site
default_run_options[:pty] = true
# =================================== #
# CAPISTRANO RECIPE #
# =================================== #
namespace :deploy do
# Deployment Tasks
desc "Executes the initial procedures for deploying a Ruby on Rails Application."
task :initial do
system "cap deploy:setup"
system "cap deploy"
system "cap deploy:db:create"
system "cap deploy:db:migrate"
system "cap deploy:passenger:restart"
end
namespace :passenger do
desc "Restarts Passenger"
task :restart do
puts "\n\n=== Restarting Passenger! ===\n\n"
run "#{ try_sudo } touch #{ File.join(current_path, 'tmp', 'restart.txt') }"
end
end
# Database Tasks
namespace :db do
desc "Create Production Database"
task :create do
puts "\n\n=== Creating the Production Database! ===\n\n"
run "cd #{current_path}; rake db:create RAILS_ENV=production"
end
desc "Migrate Production Database"
task :migrate do
puts "\n\n=== Migrating the Production Database! ===\n\n"
run "cd #{current_path}; rake db:migrate RAILS_ENV=production"
end
desc "Resets the Production Database"
task :migrate_reset do
puts "\n\n=== Resetting the Production Database! ===\n\n"
run "cd #{current_path}; rake db:migrate:reset RAILS_ENV=production"
end
desc "Destroys Production Database"
task :drop do
puts "\n\n=== Destroying the Production Database! ===\n\n"
run "cd #{current_path}; rake db:drop RAILS_ENV=production"
end
desc "Populates the Production Database"
task :seed do
puts "\n\n=== Populating the Production Database! ===\n\n"
run "cd #{current_path}; rake db:seed RAILS_ENV=production"
end
end
# Repository Tasks
namespace :repository do
desc "Creates the remote Git repository."
task :create do
puts "\n\n=== Creating remote Git repository! ===\n\n"
run "mkdir -p #{repository_path}"
run "cd #{repository_path} && git --bare init"
system "git remote rm origin"
system "git remote add origin #{repository[:repository]}"
p "#{repository[:repository]} was added to your git repository as origin/master."
end
desc "Destroys the remote Git repository."
task :destroy do
puts "\n\n=== destroying remote Git repository! ===\n\n"
run "rm -rf #{repository_path}"
system "git remote rm origin"
p "#{repository[:repository]} (origin/master) was removed from your git repository."
end
desc "Resets the remote Git repository."
task :reset do
puts "\n\n=== Resetting remove Git repository! ===\n\n"
system "cap deploy:repository:destroy"
system "cap deploy:repository:create"
end
desc "Reinitializes Origin/Master."
task :reinitialize do
system "git remote rm origin"
system "git remote add origin #{repository[:repository]}"
p "#{repository[:repository]} (origin/master) was added to your git repository."
end
end
# Environment Tasks
namespace :environment do
desc "Creates the production environment"
task :create do
system "cap deploy:setup"
end
desc "Destroys the production environment"
task :destroy do
run "rm -rf #{deploy_to}"
end
desc "Resets the production environment"
task :reset do
run "rm -rf #{deploy_to}"
system "cap deploy:setup"
end
end
end
# Callbacks
after "deploy", "deploy:cleanup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment