Create a gist now

Instantly share code, notes, and snippets.

@twetzel /deploy.rb forked from Jesus/deploy.rb
Last active Jul 19, 2017

What would you like to do?
compile assets local with capistrano 3.2.1 and rails 4.1.1 (fully integrated)
# Clear existing task so we can replace it rather than "add" to it.
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'
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"
# 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}" }
# clean up
run_locally { execute "rm -rf #{local_dir}" }

twetzel commented Sep 1, 2014

did some changes to completely integrate in usual capistrano flow

  • with rails_env: fetch(:stage) doesn't work on my machine, so removed it
  • upload to release_path instead of shared_path (capistrano will do the rest and rollback works!)
  • put in deploy namespace so loads like default
  • keep * deploy:assets:backup_manifest* so rollback works

I'm using Rails 4.1.1, Capistrano 3.2.1


twetzel commented Sep 1, 2014

Big thank you to everybody involved in the process, also look at this versions:

ealden commented Sep 24, 2014

@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

snow commented Jul 3, 2015

works fine! thanks man!

and I forked my version
because my projects usually have a "sandbox" stage, which is run under "production" environment

pratik60 commented Sep 3, 2015

@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:


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.

dsandstrom commented May 27, 2016 edited

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.

Yet another local precompile deploy script

Works great, thank you.

Buena bola! (Spanish for, "good one")
Works great, thank you!

Mavvie commented Jul 19, 2017

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