Skip to content

Instantly share code, notes, and snippets.

@mreinsch
Last active December 10, 2015 00:28
Show Gist options
  • Save mreinsch/4350889 to your computer and use it in GitHub Desktop.
Save mreinsch/4350889 to your computer and use it in GitHub Desktop.
Speed up asset compilation by simply coping assets if nothing changed (based on git). Also works fine with turbo_sprockets. Load this with: load 'simple_assets' in your deploy.rb
load 'deploy' unless defined?(_cset)
_cset :asset_env, "RAILS_GROUPS=assets"
_cset :assets_prefix, "assets"
_cset :assets_role, [:web]
_cset :normalize_asset_timestamps, false
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 => assets_role, :except => { :no_release => true } do
run "cd #{latest_release} && cp -a #{previous_release}/public/assets public/ && cp -a #{previous_release}/tmp/cache tmp/"
updated_assets = capture("cd #{latest_release} && git show --pretty=format: --name-only #{previous_revision}..#{latest_revision} | grep -c ^app/assets/ || true").to_i
if updated_assets > 0
logger.info("#{updated_assets} updated assets. Will precompile.")
run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile"
else
logger.info("No updated assets, using assets from previous release.")
end
end
desc <<-DESC
Run the asset clean rake task. Use with caution, this will delete \
all of your compiled assets. 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 :clean, :roles => assets_role, :except => { :no_release => true } do
run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:clean"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment