Skip to content

Instantly share code, notes, and snippets.

@synth
Last active December 17, 2015 06:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synth/5566239 to your computer and use it in GitHub Desktop.
Save synth/5566239 to your computer and use it in GitHub Desktop.
Local Asset Precompilation with Capistrano and AssetSync
#
# = Capistrano Assets recipe
#
# Provides a couple of tasks for precompiling assets locally
# Assumes usage of AssetSync to deploy to CloudProvider or CDN
#
# Inspired by http://www.rostamizadeh.net/blog/2012/04/14/precompiling-assets-locally-for-capistrano-deployment
namespace :deploy do
namespace :assets do
desc <<-DESC
Precompiles assets locally. If you are using AssetSync, it should send them
to the cloud automatically.
DESC
task :precompile, roles: :web do
# i keep this in so that it triggers login at the beginning of the process
from = source.next_revision(current_revision)
run_locally("rake assets:clean && rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
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"
run_locally("rake assets:clean")
end
desc <<-DESC
Sync assets to the cloud via Asset Sync. Must be run remotely(so it pulls remote aws credentials)
and after code is deployed so it gets proper asset manifest to sync
DESC
task :sync, roles: :web do
run("cd #{latest_release} && /usr/bin/env rake assets:sync RAILS_ENV=production")
end
desc <<-DESC
[internal] Updates the symlink for assets
DESC
task :symlink, roles: :web do
run ("rm -rf #{latest_release}/public/assets &&
mkdir -p #{latest_release}/public &&
mkdir -p #{shared_path}/assets &&
ln -s #{shared_path}/assets #{latest_release}/public/assets")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment