capistrano/capistrano — Issue #150: deploy:cleanup task gets out of sync
Author: wr0ngway
Created: 2012-01-31 12:08:06+00:00
State: open

If I do targeted deploys to specific subsets of hosts, the deploy:cleanup stops working because it uses capture to get the list of directories to clean from the first host only.

The following fixes this behavior for me, but may need some tweaking (sh instead of bash) for wider compatibility:

task :cleanup, :except => { :no_release => true } do

count = fetch(:keep_releases, 5).to_i

sudo "bash -c 'all=$(ls -x1 #{releases_path} | sort -n); keep=$(ls -x1 #{releases_path} | sort -n | tail -n #{count}); remove=$(comm -23 <(echo -e \"$all\") <(echo -e \"$keep\")); for r in $remove; do echo rm -rf #{releases_path}/$r; done;'"

end