Ruby Capistrano配方,仅在需要时自动运行部署:清理

Ruby Capistrano配方,仅在需要时自动运行部署:清理,ruby,deployment,capistrano,webistrano,Ruby,Deployment,Capistrano,Webistrano,我们每天使用capistrano进行20多次部署(事实上),我们的服务器上的磁盘空间充满了旧的部署文件夹 我时不时地运行deploy:cleanup任务来清除所有部署(它保留最后的:keep_releases,当前设置为30)。我想自动清理 一种解决方案是在配方中添加以下内容,以便在每次部署后自动运行清理: after "deploy", "deploy:cleanup" 但是,我不想在每次部署后都这样做,我只想将其限制在以前部署的数量达到阈值时,例如70。有人知道我怎么做吗 想法: Ca

我们每天使用capistrano进行20多次部署(事实上),我们的服务器上的磁盘空间充满了旧的部署文件夹

我时不时地运行
deploy:cleanup
任务来清除所有部署(它保留最后的
:keep_releases
,当前设置为30)。我想自动清理

一种解决方案是在配方中添加以下内容,以便在每次部署后自动运行清理:

after "deploy", "deploy:cleanup"
但是,我不想在每次部署后都这样做,我只想将其限制在以前部署的数量达到阈值时,例如70。有人知道我怎么做吗


想法:

  • Capistrano是否提供一个变量来保存以前部署的数量?
    • 如果没有,有人知道计算方法吗。i、 e.
      set:num\u发布,
  • 是否有办法拉皮条
    部署:清理
    以使用最小阈值,即如果
    :max\u释放
    以前的部署(其中
    :max\u释放
    不同于
    :keep\u释放
    ),则退出
  • 是否可以使用除关键字之外的
    ?i、 类似于
    :except=>{:num\u发布<70}

使用当前capistrano代码的快速而肮脏的方法:

将中的清理任务更改为:

  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
到您的部署配方

Capistrano是否提供一个变量来保存以前部署的数量

是,
发布。长度

有没有一种方法可以让pimp部署:cleanup使用最小阈值

是的,这里有一个私人命名的任务,只有在建立了一定数量的发行版文件夹时才会触发正常清理任务:

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"
这方面的好处是,
deploy:cleanup
上设置的指令之后,
之前和
之后都会正常执行。例如,我们要求:

before 'deploy:cleanup', 'mystuff:prepare_cleanup_permissions'
after 'deploy:cleanup', 'mystuff:restore_cleanup_permissions'