我们每天使用capistrano进行20多次部署(实际上是webistrano),我们遇到的问题是服务器上的磁盘空间充满了旧的部署文件夹。
我不时地运行deploy:cleanup
任务来清除所有部署(它保留最后:keep_releases
,当前设置为30)。我想自动清理。
一种解决方案是将以下内容添加到配方中,以便在每次部署后自动运行清理:
after "deploy", "deploy:cleanup"
但是,我不希望在每次部署之后执行此操作,我想将其限制为仅在先前部署的数量达到threashold时,例如70.有谁知道我怎么做到这一点?
思想:
set :num_releases, <what-can-I-put-here-to-count-previous-deployments>
deploy:cleanup
使用最低门槛,即< :max_releases
之前的部署退出(:max_releases
与:keep_releases
不同)。except
关键字?即:except => { :num_releases < 70}
。答案 0 :(得分:4)
Capistrano是否提供了一个包含先前部署数量的变量?
是的,releases.length
有没有办法进行pimp部署:清理以便使用最低阈值?
是的,这是一个私有命名空间的任务,只有在建立了一定数量的版本文件夹时才会触发正常的清理任务:
namespace :mystuff do
task :mycleanup, :except => { :no_release => true } do
thresh = fetch(:cleanup_threshold, 70).to_i
if releases.length > thresh
logger.info "Threshold of #{thresh} releases reached, runing deploy:cleanup."
deploy.cleanup
end
end
end
要在部署后自动运行,请将其放在配方的顶部:
after "deploy", "mystuff:mycleanup"
关于这一点的好处是,在before
上设置的after
和deploy:cleanup
指令正常执行。例如,我们需要以下内容:
before 'deploy:cleanup', 'mystuff:prepare_cleanup_permissions'
after 'deploy:cleanup', 'mystuff:restore_cleanup_permissions'
答案 1 :(得分:0)
使用当前capistrano代码快速而肮脏的方法:
将https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb#L405中的清理任务更改为:
task :cleanup, :except => { :no_release => true } do
thresh = fetch(:cleanup_threshold, 70).to_i
count = fetch(:keep_releases, 5).to_i
if thresh >= releases.length
logger.important "no old releases to clean up"
else
logger.info "threshold of #{thresh} releases reached, keeping #{count} of #{releases.length} deployed releases"
directories = (releases - releases.last(count)).map { |release|
File.join(releases_path, release) }.join(" ")
try_sudo "rm -rf #{directories}"
end
end
然后你就可以添加
了set :cleanup_threshold, 70
到您的部署配方。