Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The idea is that if you don't change your assets you don't need to recompile them each time:</p> <p>This is the <a href="http://www.bencurtis.com/2011/12/skipping-asset-compilation-with-capistrano/" rel="noreferrer">solution that Ben Curtis propose</a> for a deployment with git:</p> <pre><code> namespace :deploy do namespace :assets do task :precompile, :roles =&gt; :web, :except =&gt; { :no_release =&gt; true } do from = source.next_revision(current_revision) if releases.length &lt;= 1 || capture("cd #{latest_release} &amp;&amp; #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i &gt; 0 run %Q{cd #{latest_release} &amp;&amp; #{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 end </code></pre> <p>Here is another approach based on asset age (<a href="https://gist.github.com/2784462" rel="noreferrer">https://gist.github.com/2784462</a>) :</p> <pre><code>set :max_asset_age, 2 ## Set asset age in minutes to test modified date against. after "deploy:finalize_update", "deploy:assets:determine_modified_assets", "deploy:assets:conditionally_precompile" namespace :deploy do namespace :assets do desc "Figure out modified assets." task :determine_modified_assets, :roles =&gt; assets_role, :except =&gt; { :no_release =&gt; true } do set :updated_assets, capture("find #{latest_release}/app/assets -type d -name .git -prune -o -mmin -#{max_asset_age} -type f -print", :except =&gt; { :no_release =&gt; true }).split end desc "Remove callback for asset precompiling unless assets were updated in most recent git commit." task :conditionally_precompile, :roles =&gt; assets_role, :except =&gt; { :no_release =&gt; true } do if(updated_assets.empty?) callback = callbacks[:after].find{|c| c.source == "deploy:assets:precompile" } callbacks[:after].delete(callback) logger.info("Skipping asset precompiling, no updated assets.") else logger.info("#{updated_assets.length} updated assets. Will precompile.") end end end end </code></pre> <p>If you prefer to precompile your assets locally you can use this task:</p> <pre><code>namespace :deploy do namespace :assets do desc 'Run the precompile task locally and rsync with shared' task :precompile, :roles =&gt; :web, :except =&gt; { :no_release =&gt; true } do from = source.next_revision(current_revision) if releases.length &lt;= 1 || capture("cd #{latest_release} &amp;&amp; #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i &gt; 0 %x{bundle exec rake assets:precompile} %x{rsync --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{host}:#{shared_path}} %x{bundle exec rake assets:clean} else logger.info 'Skipping asset pre-compilation because there were no asset changes' end end end end </code></pre> <p>Another interesting approach can be that of using a <strong>git hook</strong>. For example you can add this code to <code>.git/hooks/pre-commit</code> which checks if there are any differences in the assets files and eventually precompiles them and add them to the current commit.</p> <pre><code>#!/bin/bash # source rvm and .rvmrc if present [ -s "$HOME/.rvm/scripts/rvm" ] &amp;&amp; . "$HOME/.rvm/scripts/rvm" [ -s "$PWD/.rvmrc" ] &amp;&amp; . "$PWD/.rvmrc" # precompile assets if any have been updated if git diff-index --name-only HEAD | egrep '^app/assets' &gt;/dev/null ; then echo 'Precompiling assets...' rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets git add public/assets/* fi </code></pre> <p>If you decide to use this approach you would probably need to change your <code>config/environments/development.rb</code> adding:</p> <pre><code>config.assets.prefix = '/assets_dev' </code></pre> <p>So that while in development you won't serve the precompiled assets.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload