# 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 |
Big thank you to everybody involved in the process, also look at this versions:
@twetzel this is great. Thanks!
Just some questions:
- I have my "web" and "app" servers in different machines. What I noticed is that since
manifest.json
is not generated in the app server, Rails does not use the precompiled paths. I got around this by compiling the assets locally, and then rsyncing them to both web and app servers. Is this something that you've encountered? - Also, when defining a "web" role that is different from "app", Rails also checks out the code etc. I wanted web to simply be a folder that contains assets with no Rails code or even Ruby installed. I ended up not defining a "web" role and simply defining an "assets_host" (not the best name) variable for this. Same situation as yours?
@twetzel Won't the --delete on rsync delete the old precompiled files that you would fall back to in case of an error?
Nevermind, I had those linked/shared
Works fine 😄 Thanks man
works fine! thanks man!
and I forked my version https://gist.github.com/snow/f3ea4943ccddb3a83836
because my projects usually have a "sandbox" stage, which is run under "production" environment
@twetzel - Is there any way we can integrate the feature to skip asset compilation if assets haven't changed?
Thanks, this is helpful.
@pratik60 Rails caches your assets so compiling unchanged assets is fast, even on the server.
Capistrano Rails adds public/assets
to linked_dirs
so I had to clear the task:
Rake::Task["deploy:set_linked_dirs"].clear
I'm using unicorn and zero downtime deployment and I'm having a problem with the assets getting removed from the previous release before the new release clicks in. The assets are getting rsynced to the release folder, so I think it has to do with the manifest file.
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 runs Manifest#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 uses Dir.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.
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!
did some changes to completely integrate in usual capistrano flow
I'm using Rails 4.1.1, Capistrano 3.2.1