My Rails 4.1.8 app is misbehaving in production. When I deploy a new release of my app for some odd reason all of my assets are re-fingerprinted even when the assets haven't changed. This is essentially bursting the cache for all assets on every deploy. Clearly, this defeats one of the purposes of using the asset pipeline in the first place.

I'm using:

  • Rails 4.1.8, Ruby 2.2.2
  • Capistrano 3.4 for deployment.
  • sprockets-rails 2.3.1

Excerpt from production.rb:

config.assets.digest = true
config.serve_static_assets = true

config/initializers/assets.rb

Rails.application.config.assets.version = '1.0'

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rbenv'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

set :rbenv_type, :user 
set :rbenv_ruby, '2.2.2'

/config/deploy.rb

lock '3.4'
set :application, 'myapp'
set :repo_url, '[email protected]:user/app.git'
set :deploy_to, '/home/deploy/app'

set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'tmp/uploads', 'vendor/bundle', 'public/system')
set :bundle_binstubs, nil
set :keep_releases, 5

SSHKit.config.command_map[:rake]  = 'bundle exec rake'
SSHKit.config.command_map[:rails] = 'bundle exec rails'

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      within "#{release_path}" do
        execute :bundle, "exec thin restart -O -C /etc/thin/app.yml"
    end
  end
end

after :publishing, :restart

Upon deployment:

Me$: bin/cap production deploy

...
DEBUG [ff4ce05c] Running /usr/bin/env if test ! -d /home/deploy/app/releases/20150618015809; then echo "Directory does not exist '/home/deploy/app/releases/20150618015809'" 1>&2; false; fi as [email protected]
DEBUG [ff4ce05c] Command: if test ! -d /home/deploy/app/releases/20150618015809; then echo "Directory does not exist '/home/deploy/app/releases/20150618015809'" 1>&2; false; fi
DEBUG [ff4ce05c] Finished in 0.370 seconds with exit status 0 (successful).
INFO [4ab0f9c7] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as [email protected]
DEBUG [4ab0f9c7] Command: cd /home/deploy/app/releases/20150618015809 && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=staging ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.892846 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-7c9c7de5d4c12790ac074e1c15637315.eot
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.894641 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-8252ebfc2dfe42e154d8e8bcedbfd2f6.svg
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.896158 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-cf546ab4766383966d7b70bd2749c011.ttf
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.897666 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-4bafadbafe1d7f0915ef05f107e97a25.woff
DEBUG [4ab0f9c7]    I, [2015-06-18T01:58:31.899064 #16404]  INFO -- : Writing /home/deploy/app/releases/20150618015809/public/assets/proximanova-bold-3b37298fc9eb69093953f9885e889090.woff2
...

Then I run for a second time (without changing any asset)

Me$: bin/cap production deploy

...
DEBUG [cbbe9a55] Running /usr/bin/env if test ! -d /home/deploy/app/releases/20150618020326; then echo "Directory does not exist '/home/deploy/app/releases/20150618020326'" 1>&2; false; fi as [email protected]
DEBUG [cbbe9a55] Command: if test ! -d /home/deploy/app/releases/20150618020326; then echo "Directory does not exist '/home/deploy/app/releases/20150618020326'" 1>&2; false; fi
DEBUG [cbbe9a55] Finished in 0.369 seconds with exit status 0 (successful).
INFO [6e1eeadf] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as [email protected]
DEBUG [6e1eeadf] Command: cd /home/deploy/app/releases/20150618020326 && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=staging ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.365574 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-78ec1c34931362f5fc9d1c64477d075f.eot
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.367236 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-6fadee20c88b51ed286db9687b131999.svg
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.368709 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-dc42499d91c401ef2e3452caa1917adf.ttf
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.370105 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-2d17c273eabf9ce851993897dd1fb300.woff
DEBUG [6e1eeadf]    I, [2015-06-18T02:03:48.371464 #16929]  INFO -- : Writing /home/deploy/app/releases/20150618020326/public/assets/proximanova-bold-7562093954d44696b2df7ada1488e2da.woff2
...

Clearly, all of the assets have been re-fingerprinted which shouldn't be the case.

If I log into the production server, delete all of the assets in /public/assets and run the following:

RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.2 RAILS_ENV=production ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile

The assets are compiled correctly. If I run the same command again, the assets are not regenerated, which again is the correct behaviour. Just can't to seem to get the correct behaviour when deploying via Capistrano.

Could be doing something blatantly incorrect in the config, but so far I've been unable to narrow it down despite the myriad of config options I've experimented with.

Any ideas Rails gurus?

2

There are 2 answers

2
will_in_wi On

Could you try downloading one of the files, redeploying, and then downloading again? Run a checksum (MD5, SHA256, something) on the two files and see whether they match.

The checksums are based on the contents of the files. So if the files are getting different checksums, they probably have different contents somehow. You can run a diff on two of them, probably the SVG since the XML will diff better.

0
Jaybloke On

I discovered the route of the problem... my angular-rails-templates gem - https://github.com/pitr/angular-rails-templates.

Versions 0.1.5 and 0.2.0 of the gem appends a checksum to the config.assets.version string every time the JS templates are compiled. This was busting the cache on every deploy even if no assets had changed.

The temporary solution is to downgrade to version 0.1.4 until a fix can be provided. See https://github.com/pitr/angular-rails-templates/issues/104