assets本地编译优化部署时间

随着rails项目js效果增加,加了JS MVC框架等,变的每发布一次时间越来越延长!也增加服务器在高峰时候cpu与内存的负担!

综合考虑解决把 rake assets:precompile 放在本地编译,然后通过capistrano2发布到服务端!

修改 config/deploy.rb

namespace :prepare do
  desc "Compress assets in a local file"
  task :compress_assets do
    run_locally("rm -rf public/assets/*")
    run_locally("bundle exec rake assets:precompile")
    run_locally("touch assets.tgz && rm assets.tgz")
    run_locally("tar zcvf assets.tgz public/assets/")
    run_locally("mv assets.tgz public/assets/")
  end

  desc "Upload assets"
  task :upload_assets do
    upload("public/assets/assets.tgz", release_path + '/assets.tgz')
    run "cd #{release_path}; tar zxvf assets.tgz; rm assets.tgz"
  end
end
  before "deploy:update_code", "prepare:compress_assets"
  ...
  after "deploy:symlink", "prepare:upload_assets"

#####更新补充:

发现这个capistrano-local-precompile gem可以本地compile再同步到server上!

Comments