我用capistrano在本地预编译我的资产,然后用rsync上传这些资产,下一个代码在deploy.rb:
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_locally("rm -rf public/assets/*")
run_locally "bundle exec rake assets:precompile"
find_servers_for_task(current_task).each do |server|
#clean server assets before upload local new assets
run_locally "rsync -vr --exclude='.DS_Store' --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{server.host}:#{shared_path}/"
end
else
puts "Skipping asset pre-compilation because there were no asset changes"
end
end
end
问题是文件夹#{shared_path}/assets
随着我所做的每次部署而快速增长。
我需要在生产服务器上运行代码rm -rf public/assets/*
,然后在线上上传本地新资产#clean server assets before upload local new assets
我该怎么做?
把这个添加到config/deploy.rb
# Callbacks
namespace :deploy do
before :compile_assets, :force_cleanup_assets do
on release_roles(fetch(:assets_roles)) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, 'assets:clobber'
end
end
end
end
end
您只需要创建一个任务并在:precompile任务之前运行它。
task :clean_assets do
...
end
// tell capistrano when you want to run this task
before 'precompile', 'clean_assets
当文件夹非常大时删除资源的修复方法是:
run %Q{cd #{shared_path} && rm -rf assets/* }
之前,如果您愿意,您已经设置了共享文件夹的路径:
set :shared_path, "path_to_your_shared_folder"
这个问题的最终代码如下:
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_locally("rm -rf public/assets/*")
run_locally "bundle exec rake assets:precompile"
find_servers_for_task(current_task).each do |server|
run %Q{cd #{shared_path} && rm -rf assets/* }
run_locally "rsync -vr --exclude='.DS_Store' --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{server.host}:#{shared_path}/"
end
else
puts "Skipping asset pre-compilation because there were no asset changes"
end
end
end