Skip to content

Instantly share code, notes, and snippets.

@bipinshashi
Created June 22, 2014 21:43
Show Gist options
  • Save bipinshashi/703792fa31a5fb0f46f3 to your computer and use it in GitHub Desktop.
Save bipinshashi/703792fa31a5fb0f46f3 to your computer and use it in GitHub Desktop.
if Rubber::Util.has_asset_pipeline?
# load 'deploy/assets'
_cset :asset_env, "RAILS_GROUPS=assets"
_cset :assets_prefix, "assets"
_cset :assets_role, [:web]
_cset :normalize_asset_timestamps, false
before 'deploy:finalize_update', 'deploy:assets:symlink'
after 'deploy:update_code', 'deploy:assets:precompile'
namespace :deploy do
namespace :assets do
desc <<-DESC
Run the asset precompilation rake task. You can specify the full path \
to the rake executable by setting the rake variable. You can also \
specify additional environment variables to pass to rake via the \
asset_env variable. The defaults are:
set :rake, "rake"
set :rails_env, "production"
set :asset_env, "RAILS_GROUPS=assets"
DESC
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
ast = `#{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/`.strip rescue "1" #always
if ast.length > 0 or ENV['FORCE_ASSETS']
logger.info "Precompiling assets locally..."
run_locally("RAILS_ENV=production rake assets:clean && RAILS_ENV=production rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
logger.info "#{shared_path}"
top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
run_locally "rm public/assets.tar.bz2 && rm -r public/assets"
run_locally("RAILS_ENV=production rake assets:clean && true")
else
logger.info "Skipping asset precompilation because there were no asset changes"
end
end
desc <<-DESC
[internal] This task will set up a symlink to the shared directory \
for the assets directory. Assets are shared across deploys to avoid \
mid-deploy mismatches between old application html asking for assets \
and getting a 404 file not found error. The assets cache is shared \
for efficiency. If you customize the assets path prefix, override the \
:assets_prefix variable to match.
DESC
task :symlink, :roles => :web, :except => { :no_release => true } do
run <<-CMD
rm -rf #{latest_release}/public/#{assets_prefix} &&
mkdir -p #{latest_release}/public &&
mkdir -p #{shared_path}/assets &&
ln -s #{shared_path}/assets #{latest_release}/public/#{assets_prefix}
CMD
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