Skip to content

Instantly share code, notes, and snippets.

@matenia
Created December 30, 2011 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matenia/1537389 to your computer and use it in GitHub Desktop.
Save matenia/1537389 to your computer and use it in GitHub Desktop.
Deploy from local project via capistrano
require "bundler/capistrano"
require 'capistrano/ext/multistage'
require 'new_relic/recipes'
set :application, "sampleapp"
set :branch, "master"
# deploy from local
set :scm, :git
set :repository, ".git"
set :deploy_via, :copy
set :use_sudo, false
set :user, 'username'
set :password, 'secret'
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :keep_releases, 3
set :stages, [ :production ]
set :default_stage, :production
role :web, "example.com"
role :app, "example.com"
role :db, "example.com", :primary => true
set :deploy_to, "/home/#{user}/#{application}"
set :bundle_flags, ""
set :bundle_dir, fetch(:shared_path) + "/vendor/bundle"
before 'deploy:assets:precompile' do
# add some stuff like replacing the database yml file with the production version
run "rm #{release_path}/config/database.yml"
run "cp #{release_path}/config/deploy/database.yml #{release_path}/config/database.yml"
end
before 'deploy:assets:precompile', 'bundle:install'
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
# passenger requires this to restart the webserver
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :migrate, :roles => :db do
run "cd #{current_path}; RAILS_ENV=#{rails_env} #{rake} db:migrate"
end
task :seed, :roles => :db do
run "cd #{current_path}; RAILS_ENV=#{rails_env} #{rake} db:seed"
end
task :fetchdb, :roles => :db do
run "cd #{current_path}; RAILS_ENV=#{rails_env} #{rake} db:data:dump"
get("#{current_path}/db/data.yml", "db/data.yml")
end
task :sitemap, :roles => :app do
run "cd #{release_path}; RAILS_ENV=#{rails_env} #{rake} sitemap:refresh"
end
end
after 'deploy:update_code' do
# should refactor this ... DRY time
run "rm #{release_path}/config/database.yml"
run "cp #{release_path}/config/deploy/database.yml #{release_path}/config/database.yml"
end
after 'deploy' do
run "ln -s /home/#{user}/#{application}/current/public /home/#{user}/public_html"
end
after 'deploy', 'deploy:cleanup'
after 'deploy', 'deploy:sitemap'
after 'deploy:setup', :roles => :app do
location = fetch(:template_dir, "config/deploy") + '/database.yml'
template = File.read(location)
config = ERB.new(template)
run "ln -s /home/#{user}/#{application}/current/public /home/#{user}/public_html"
run "touch #{File.join(current_path,'tmp','restart.txt')}"
#put config.result(binding), "#{shared_path}/config/database.yml", :via => :scp
end
desc "Remote console on the production appserver"
task :console, :roles => :app do
input = ''
run "cd #{current_path} && RAILS_ENV=#{rails_env} script/rails console" do |channel, stream, data|
next if data.chomp == input.chomp || data.chomp == ''
print data
channel.send_data(input = $stdin.gets) if data =~ /:\d{3}:\d+(\*|>)/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment