Search code examples
ruby-on-railsrubyruby-on-rails-3deploymentcapistrano

clean old assets with capistrano on production server


I precompile my assets with capistrano on locally and after upload these assets with rsync, with the next code on 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

The problem is that the folder #{shared_path}/assets is growing quickly with each deployment I do.

I need run the code rm -rf public/assets/* on production server before upload local new assets on line #clean server assets before upload local new assets

How can I do it?


Solution

  • The fix for remove assets when this folder is very large is:

    run %Q{cd #{shared_path} && rm -rf assets/* }
    

    Previously, if you wish, you have set the path to shared folder with:

    set :shared_path, "path_to_your_shared_folder"
    

    the final code for this question is as follows:

    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