Skip to content

Instantly share code, notes, and snippets.

@DaKaZ
Last active August 29, 2015 14:18
Show Gist options
  • Save DaKaZ/6f4ec97c87eddbb44604 to your computer and use it in GitHub Desktop.
Save DaKaZ/6f4ec97c87eddbb44604 to your computer and use it in GitHub Desktop.
deploy.rb for local asset precompile with rubber
# This is a sample Capistrano config file for rubber
set :rails_env, Rubber.env
set :ssh_timeout, 15
on :load do
set :application, rubber_env.app_name
set :runner, rubber_env.app_user
set :deploy_to, "/mnt/#{application}-#{Rubber.env}"
set :copy_exclude, [".git/*", ".bundle/*", "log/*", ".rvmrc", ".rbenv-version", ".idea", ".rspec", ".zeus.sock", "public/system"]
set :assets_role, [:app]
end
# Use a simple directory tree copy here to make demo easier.
# You probably want to use your own repository for a real app
set :scm, :git
set :repository, "."
set :deploy_via, :copy
# Easier to do system level config as root - probably should do it through
# sudo in the future. We use ssh keys for access, so no passwd needed
set :user, 'root'
set :password, nil
# Use sudo with user rails for cap deploy:[stop|start|restart]
# This way exposed services (mongrel) aren't running as a privileged user
set :use_sudo, true
# How many old releases should be kept around when running "cleanup" task
set :keep_releases, 3
# Lets us work with staging instances without having to checkin config files
# (instance*.yml + rubber*.yml) for a deploy. This gives us the
# convenience of not having to checkin files for staging, as well as
# the safety of forcing it to be checked in for production.
set :push_instance_config, Rubber.env != 'production'
# don't waste time bundling gems that don't need to be there
set :bundle_without, [:development, :test, :staging] if Rubber.env == 'production'
# Allow us to do N hosts at a time for all tasks - useful when trying
# to figure out which host in a large set is down:
# RUBBER_ENV=production MAX_HOSTS=1 cap invoke COMMAND=hostname
max_hosts = ENV['MAX_HOSTS'].to_i
default_run_options[:max_hosts] = max_hosts if max_hosts > 0
# Allows the tasks defined to fail gracefully if there are no hosts for them.
# Comment out or use "required_task" for default cap behavior of a hard failure
rubber.allow_optional_tasks(self)
# namespace :deploy do
# namespace :assets do
# rubber.allow_optional_tasks(self)
# tasks.values.each do |t|
# if t.options[:roles]
# task t.name, t.options, &t.body
# end
# end
# end
# end
# load in the deploy scripts installed by vulcanize for each rubber module
Dir["#{File.dirname(__FILE__)}/rubber/deploy-*.rb"].each do |deploy_file|
load deploy_file
end
# make sure assets are compiled
after "deploy:finalize_update", "deploy:assets:precompile"
# capistrano's deploy:cleanup doesn't play well with FILTER
after "deploy", "cleanup"
after "deploy:migrations", "cleanup"
task :cleanup, :except => { :no_release => true } do
count = fetch(:keep_releases, 5).to_i
rsudo <<-CMD
all=$(ls -x1 #{releases_path} | sort -n);
keep=$(ls -x1 #{releases_path} | sort -n | tail -n #{count});
remove=$(comm -23 <(echo -e "$all") <(echo -e "$keep"));
for r in $remove; do rm -rf #{releases_path}/$r; done;
CMD
end
# We need to ensure that rubber:config runs before asset precompilation in Rails, as Rails tries to boot the environment,
# which means needing to have DB access. However, if rubber:config hasn't run yet, then the DB config will not have
# been generated yet. Rails will fail to boot, asset precompilation will fail to complete, and the deploy will abort.
if Rubber::Util.has_asset_pipeline?
load 'deploy/assets'
# Wrap tasks in the deploy namespace that have roles so that we can use FILTER
# with something like a deploy:cold which tries to run deploy:migrate but can't
# because we filtered out the :db role
namespace :deploy do
# See http://sourcey.com/precompiling-assets-for-large-rails-deployments-with-capistrano/
namespace :assets do
desc "Skipping the update of asset mtimes"
task :update_asset_mtimes do
# do nothing...
end
desc "Precompile assets locally and then rsync to deploy server"
task :precompile, :only => { :primary => true } do
run_locally "bundle exec rake assets:precompile"
servers = find_servers :roles => [:app], :except => { :no_release => true }
servers.each do |server|
run_locally "rsync -av ./public/#{assets_prefix}/ #{user}@#{server}:#{current_path}/public/#{assets_prefix}/"
end
run_locally "git clean -fd public/#{assets_prefix}"
end
end
rubber.allow_optional_tasks(self)
tasks.values.each do |t|
if t.options[:roles]
task t.name, t.options, &t.body
end
end
end
callbacks[:after].delete_if {|c| c.source == "deploy:assets:precompile"}
#callbacks[:before].delete_if {|c| c.source == "deploy:assets:symlink"}
#before "deploy:assets:precompile", "deploy:assets:symlink"
after "rubber:config", "deploy:assets:precompile"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment