Asset Precompile on the development machine before capistrano deployment

心已入冬 提交于 2019-12-10 17:18:06

问题


I want the asset precompile to happen on my dev machine beofore the code is packed (tar ball'ed) by capistrano and have the precompiled assets already included in the final deployment package.

When I try the inbuilt capistrano recipe thats in load 'deploy/assets' it runs rake RAILS_GROUPS=assets assets:precompile on the server.

The reason I am looking for this because at the moment the precompile is taking too long on my EC2 micro-instance (and also at times just hangs), It would great if asset compile could be done even before the deploy starts so that I can save the server from this heavy duty work load - until at least I have better configured servers available.


回答1:


The workflow is still a little bumpy at the moment, but you may find some success using Guard-Rails-Assets. It's a little slow, especially if you are making a lot of asset changes, but it will compile assets when they are changed and you can just check them in to your repo to be deployed later.




回答2:


I've just written a gem to solve this problem inside Rails, called turbo-sprockets-rails3. It speeds up your assets:precompile by only recompiling changed files, and only compiling once to generate all assets. It works out of the box for Capistrano, since your assets directory is shared between releases.

It would be really awesome if you could help me test out the turbo-sprockets-rails3 gem, and let me know if you have any problems.




回答3:


Remove load 'deploy/assets' from Capfile or config/deploy.rb, and add the following lines to the config/deploy.rb:

set :assets_role, [ :web, :app ]
set :normalize_asset_timestamps, false
set :assets_tar_path, "#{release_name}-assets.tar.gz"

before "deploy:update" do
  run_locally "rake assets:precompile"
  run_locally "cd public; tar czf #{Dir.tmpdir}/#{assets_tar_path} assets"
end

before "deploy:finalize_update", :roles => assets_role, :except => { :no_release => true } do
  upload "#{Dir.tmpdir}/#{assets_tar_path}", "#{shared_path}/#{assets_tar_path}"
  run "cd #{shared_path}; /bin/tar xzf #{assets_tar_path}"
  run "/bin/ln -s #{shared_path}/assets #{release_path}/public"
  run "/bin/rm #{shared_path}/#{assets_tar_path}"
end

If you use turbo-sprockets-rails3, add this to the last block:

  run "cd #{release_path}; #{rake} assets:clean_expired 2> /dev/null"


来源:https://stackoverflow.com/questions/8258336/asset-precompile-on-the-development-machine-before-capistrano-deployment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!