在生产服务器上使用capistrano清理旧资产

时间:2013-01-15 14:39:23

标签: ruby-on-rails ruby ruby-on-rails-3 deployment capistrano

我在本地使用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上传本地新资产

我该怎么做?

3 个答案:

答案 0 :(得分:3)

只需将其添加到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

答案 1 :(得分:2)

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

task :clean_assets do 
  ...
end

// tell capistrano when you want to run this task
before 'precompile', 'clean_assets

答案 2 :(得分: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