-
-
Save twetzel/66de336327f79beac0e0 to your computer and use it in GitHub Desktop.
compile assets local with capistrano 3.2.1 and rails 4.1.1 (fully integrated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Clear existing task so we can replace it rather than "add" to it. | |
Rake::Task["deploy:compile_assets"].clear | |
namespace :deploy do | |
desc 'Compile assets' | |
task :compile_assets => [:set_rails_env] do | |
# invoke 'deploy:assets:precompile' | |
invoke 'deploy:assets:precompile_local' | |
invoke 'deploy:assets:backup_manifest' | |
end | |
namespace :assets do | |
desc "Precompile assets locally and then rsync to web servers" | |
task :precompile_local do | |
# compile assets locally | |
run_locally do | |
execute "RAILS_ENV=#{fetch(:stage)} bundle exec rake assets:precompile" | |
end | |
# rsync to each server | |
local_dir = "./public/assets/" | |
on roles( fetch(:assets_roles, [:web]) ) do | |
# this needs to be done outside run_locally in order for host to exist | |
remote_dir = "#{host.user}@#{host.hostname}:#{release_path}/public/assets/" | |
run_locally { execute "rsync -av --delete #{local_dir} #{remote_dir}" } | |
end | |
# clean up | |
run_locally { execute "rm -rf #{local_dir}" } | |
end | |
end | |
end |
Works great, thank you.
Buena bola! (Spanish for, "good one")
Works great, thank you!
Hey! I forked this to support Rails 5.1 + Webpacker.
The code could definitely be cleaned up but it seems to work well enough for me in production!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My problem seems to be that Nginx is looking for the assets in current, but for the few seconds before Unicorn switches to the new deployment, the assets aren't there, since current is pointing to the new release.
I tried copying the previous release's assets to the new release after rsyncing. This works, but this just carries over a bunch of old assets that are no longer needed. However, I found out about
rake assets:clean
that removes old assets. This task runsManifest#clean
which relies upon the old assets being in the manifest file. Since we are generating a new one each time, I can't clean the old ones using the rake task.I tried using the shared assets directory and not using delete when using
rsync
. This doesn't work because a new manifest is generated and Sprockets usesDir.entries.find
to figure out the right manifest file to use, which is not guaranteed to be the latest one.My solution is to use shared assets, but download the manifest file before compiling locally. This way, a new manifest is not generated. This makes the app always use the latest assets and gives Sprockets a complete list of old assets that it can clean up.
Yet another local precompile deploy script