hyperrjas
hyperrjas

Reputation: 10744

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?

Upvotes: 1

Views: 3396

Answers (3)

MrWEST
MrWEST

Reputation: 168

Just add this to 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

Upvotes: 5

hyperrjas
hyperrjas

Reputation: 10744

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

Upvotes: 2

Catfish
Catfish

Reputation: 19284

You just need to create a task and run it before the :precompile task.

task :clean_assets do 
  ...
end

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

Upvotes: 2

Related Questions