Rails local asset:precompile - is there an automated way to check for changes?

You can find the source for the precompile task in sprockets-rails. The reason why you're not seeing any changes locally may be due to fingerprinting, which is enabled by default in production and disabled in the other environments (as debugging with fingerprints can be a hassle). You can enable it using config.assets.digest as described in the assets guide

As you mentioned, it's easy to forget the precompilation step. An elegant automation would be to remove the compiled assets from your repo and to add a capistrano (or CI) task to precompile assets with each deployment. The precompilation would ideally happen on the CI server (as opposed to running it on each production server). This approach also alleviates the need to keep scanning the compiled assets for changes (which you really shouldn't have to care about).

Automatically committing anything to your repo is a bad idea - in addition to inadvertent commits, you'd end up convoluting your commit history.


My answer is Git-based, an implementation of the OP's thought 2: can we fail the build based on Git-metadata telling-us the assets/ source is newer than the assets-precompile/ currently checked-in. The answer might be able to be improved based on conditions in the check-out environment, or some knowledge of the Rails build.

  1. git log -1 --format=%ct -- assets/ will give you the timestamp of the latest source commit.
  2. git log -1 --format=%ct -- assets-precompile/ will give you the timestamp of the most recent pre-compile check-in.

Add a condition early in the build which compares these two numbers, and if 1>=2, fail any release build. Actually, you may want to build in a grace period there, in-case someone slips-in with a commit in-between when you checkout and run the pre-compile, and when you commit.

Alternatively, to be stricter with this, you would store the commit-hash of {{assets/}} in a file, and if it changes and differs from that at the last pre-compile, again, fail any release build, until it is again pre-compiled and checked-in:

git log --format=%H -- assets/

... and compare that with a commit-hash you update with your pre-commit. You might also use %T for the tree-hash instead of the commit-hash, which might simplify branching.


I'm going through the same effort, but I haven't been able to sort out this problem though. A first step is to use a git pre-commit hook to compile assets (which will only do something if there are modified assets).

http://jimneath.org/2012/05/05/precompile-assets-using-a-git-hook.html

Edit: Looks like that link got nuked, here's the archived version: https://web.archive.org/web/20161022195654/http://jimneath.org/2012/05/05/precompile-assets-using-a-git-hook.html