Skip to content

Instantly share code, notes, and snippets.

@toymachiner62
Created January 11, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toymachiner62/8373682 to your computer and use it in GitHub Desktop.
Save toymachiner62/8373682 to your computer and use it in GitHub Desktop.
Capistrano deployment example
require 'bundler/capistrano'
require 'rvm/capistrano'
set :application, 'example.com'
role :web, 'example.com'
role :app, 'example.com'
role :db, 'example.com', :primary => true
# set environment
set :rails_env, "production"
# Set this to either "exmapletest.com", or "example.com" depending on which server you want to deploy to.
set :instance, "example.com"
# set some variables based on which machine i'm on
if ENV['USER'] == "mymacuser"
set :user, "myuser"
set :password, 'mypass'
set :project_path, '/Volumes/Macintosh HD/Users/mymacuser/Sites/example'
elsif ENV['USER'] == "mywindowsuser"
set :user, 'myotheruser'
set :password, 'mypass'
set :project_path, '~/Dropbox/sites/example'
end
set :ssh_options, {:forward_agent => true}
# set the ruby version
set :rvm_ruby_string, 'ruby-1.9.3-p392'
set :rvm_type, 'webadmin'
set :port, 90
ssh_options[:forward_agent] = true
# subversion repo username and password
set :scm, :git
#set :scm_username, "git"
set :scm_passphrase, ''
set :branch, 'master'
set :scm_command, 'git'
set :deploy_via, :remote_cache # only updates changed files
set :repository, "git@example.beanstalkapp.com:/example.git"
# project info
set :server, ''
set :applicationdir, :project_path
# needed for the 'whenever' gem
#set(:whenever_command) { "RAILS_ENV=#{rails_env} bundle exec whenever"}
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
set :use_sudo, false
# specify the rvm type. We just want to use the system wide one since we're not currently specifying gemsets for each project
set :rvm_type, :system
set :rvm_path, "/usr/local/rvm"
# where to put the files
set :deploy_to, "/path/to/project/"
# fixes the "sorry, you must have a tty to run sudo" issue
default_run_options[:pty] = true
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
# chmod the files
task :after_update_code, :roles => [:web, :db, :app] do
puts "WE USING AFTER_UPDATE_CODE?"
run "#{sudo} chmod 755 #{release_path}/public -R"
end
# install new gems
desc "run bundle install and ensure all gem requirements are met"
task :install do
#run "rvm gemset use #{rvm_gemset}"
run "cd #{current_path} && bundle install --without=test"
end
# run the db migrations
task :run_migrations, :roles => :db do
puts "RUNNING DB MIGRATIONS"
#run "rvm gemset use #{rvm_gemset}"
run "cd #{current_path}; bundle exec rake db:migrate RAILS_ENV=#{rails_env} "
end
# run the data repair script
#task :add_veteran_questions, :roles => :db do
# run "cd #{current_path}; bundle exec rake add_veteran_questions RAILS_ENV=#{rails_env} "
#end
# seed the db
#task :seed, :roles => :db do
# run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env} "
#end
# precompile assets
task :precompile_assets do
run "cd #{release_path}; bundle exec rake assets:precompile RAILS_ENV=#{rails_env}"
end
task :start do ; end
task :stop do ; end
# Creates a symlink to the myrepono directory which should sit at the project root
task :symlink_myrepono, :roles => :app do
run "cd #{deploy_to}; mkdir -p myrepono" # Make the dir /myrepono if it doesn't already exist
run "cd #{current_path}/public; ln -s #{deploy_to}/myrepono/ myrepono" # Symlink the current/public/myrepono dir to project root/myrepono
end
# restart the server
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{sudo} /etc/init.d/apache2 restart"
end
task :chown_app_dir do
puts "CHOWN App Dir"
run "#{sudo} chown -R #{user} /path/to/project"
end
end
before "deploy:update", "deploy:chown_app_dir"
after("deploy:update", "deploy:run_migrations")
after("deploy:update", "deploy:precompile_assets")
after "deploy:update", "deploy:install"
after "deploy:install", "deploy:symlink_myrepono"
after "deploy:restart", "deploy:cleanup" # leave the last 5 releases only
#after "deploy:run_migrations", "deploy:add_veteran_questions"
#after "deploy:run_migrations", "deploy:seed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment