Skip to content

Instantly share code, notes, and snippets.

@col
Last active February 26, 2019 20:13
Show Gist options
  • Save col/4560969 to your computer and use it in GitHub Desktop.
Save col/4560969 to your computer and use it in GitHub Desktop.
Example Capistrano deploy file. This is a basic example that I plan to improve soon. Update 1: Added :admin_runner option to ensure deployment directories are created with the correct owner. Update 2: Added better support for multiple environments and added rails:console rails:dbconsole tasks.
require 'rvm/capistrano'
require 'bundler/capistrano'
require 'capistrano/ext/multistage'
load 'deploy/assets'
set :stages, ["staging", "production"]
set :default_stage, "production"
set :user, '?'
set :admin_runner, '?'
set :application, '?'
set :scm, 'git'
set :repository, "git@bitbucket.org:?/?.git"
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
# additional settings
default_run_options[:pty] = true # Forgo errors when deploying from windows
namespace :db do
task :setup do
run "cd #{deploy_to}/current && #{rake} db:setup RAILS_ENV=#{rails_env}"
end
end
# Passenger
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
namespace :rails do
desc "Remote console"
task :console, :roles => :app do
run_interactively "bundle exec rails console #{rails_env}"
end
desc "Remote dbconsole"
task :dbconsole, :roles => :app do
run_interactively "bundle exec rails dbconsole #{rails_env}"
end
end
def run_interactively(command, server=nil)
server ||= find_servers_for_task(current_task).first
exec "ssh -l #{user} #{server.host} -t 'source ~/.bash_profile && cd #{deploy_to}/current && #{command}'"
end
set :rvm_type, :user
set :rvm_ruby_string, '1.9.3-p362'
# server details
server '?', :app, :web, :db, :primary => true
# deploy config
set :deploy_to, "/var/www/?"
set :deploy_via, :export
set :rails_env, 'production'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment