Using deploy.rb to precompile rails assets only when they change, this task is always skipping the compile of my assets 🙁
namespace :assets do
task :precompile, :roles => :web, :except => {:no_release => true} do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
What could causing this complete task not compiling? It always thinks there are no asset changes and throws that message.
I also never really understood the task, for example what does below source.log.local refer to?
source.local.log
Could anyone clarify what the task commands do and has some pointers why it never sees any asset changes? Thank you
What it does:
sourceis a reference to your source code, as seen through your SCM (git, svn, whatever). This setsfromas (essentially) the currently deployed version of your source code.capturemeans ‘execute this command in the shell, and return its output’. The command in question references the log of changes to your source comparing the deployed version to the current version (specifying the paths where assets live as the ones that ‘matter’), and passes that into the word count tool (wc -l). The-loption means that it returns a count of the number of lines in the output. Thus, the output (which is returned bycapture) is the number of filenames that have changes between these two versions.If that number is zero, then no files have changed in any of the specified paths, so we skip precompiling.
Why it doesn’t work:
I don’t know. There doesn’t seem to be anything wrong with the code itself – it’s the same snippet I use, more or less. Here’s a couple of things that you can check:
Does Capistrano even know you’re using the asset pipeline? Check your Capfile. If you don’t have
load 'deploy/assets'in there, deploying won’t even consider compiling your assets.Have you, in fact, enabled the asset pipeline? Check application.rb for
config.assets.enabled = trueDo you have incorrect asset paths specified? The code is checking for changes in
vendor/assets/andapp/assets/. If your assets live somewhere else (lib/assets, for instance), they won’t be noticed. (If this is the case, you can just change that line to include the correct paths.Have you, in fact, changed any assets since the last deployment? I recommend bypassing the check for changed assets and forcing precompile to run once, then turning the check back on and seeing it the problem magically resolves itself. In my example, below, setting
force_precompile = truewould do that.What I use:
Here’s the version of this I currently use. It may be helpful. Or not. Changes from the original:
asset_locationsto the places your assets live)force_precompile=trueto attempt to run the check, but run precompile regardless).