Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I ran into problems with asset pre-compilation during deploys, so I opted to go for local pre-compilation and check in the resulting files to my source tree. I don't really have any problems with that approach, except that sometimes I forget to run the precompile task and release without precompiling assets! :(

I'm wondering if anyone has come across some sort of way to check to see if any asset changes have occurred? Ideally I'd like to run some sort of check on my CI server and fail the build if there's asset changes that haven't been committed.

I had a couple of thoughts:

  1. Run RAILS_ENV=production bundle exec rake assets:precompile on the CI server and see if there's any output. (The command appears to not output anything if assets are up-to-date.) However, it seems as though the command's output is tied somehow to the environment it runs in, because after running the command locally, committing the results, and then running the command on the CI server, there is still output from the command! I'd like to know why this is tied to the environment, but I can't even find the source for rake assets:precompile in the rails github repo. Does anyone know where the source for that is?

  2. Somehow write a command that can look through the git history and determine if any assets have changed within my assets/ folder since the last precompilation. Not really sure how that would work...

This has bit me more than a couple times, and sometimes I don't catch it when co-workers commit asset changes - plus it really seems like this is something a computer should be able to catch for a human. I guess a somewhat reasonable third alternative would be to have the CI server simply run the command, and commit the generated files to the source tree automatically, but I don't like the idea of my CI server making commits.

Any thoughts? Thanks.

like image 747
Mark G. Avatar asked Mar 05 '15 21:03

Mark G.


People also ask

What does Rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.


1 Answers

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.

like image 140
Musannif Zahir Avatar answered Sep 28 '22 02:09

Musannif Zahir