public
Created

Deploy from local project via capistrano

  • Download Gist
deploy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.