1

我在本地使用 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

我该怎么做?

4

3 回答 3

4

只需将其添加到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
于 2015-11-02T12:44:29.913 回答
2

您只需要创建一个任务并在 :precompile 任务之前运行它。

task :clean_assets do 
  ...
end

// tell capistrano when you want to run this task
before 'precompile', 'clean_assets
于 2013-01-15T14:54:24.107 回答
2

当此文件夹非常大时删除资产的修复方法是:

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
于 2013-01-15T16:23:45.503 回答