[分享] 频繁运行ci? 构建产物太大?教你如何清理流水线产物

删除指定日期前的job产物

确定删除对象

查找某个项目的产物

project = Project.find_by_full_path('path/to/project')
builds_with_artifacts =  project.builds.with_downloadable_artifacts

查找整个实例的产物

builds_with_artifacts = Ci::Build.with_downloadable_artifacts

删除指定日期前的产物

1.week.ago 代表一周前

builds_to_clear = builds_with_artifacts.where("finished_at < ?", 1.week.ago)
builds_to_clear.find_each do |build|
  Ci::JobArtifacts::DeleteService.new(build).execute
  build.update!(artifacts_expire_at: Time.now)
end

15.3及之前的版本使用下面的命令:

builds_to_clear = builds_with_artifacts.where("finished_at < ?", 1.week.ago)
builds_to_clear.find_each do |build|
  build.artifacts_expire_at = Time.now
  build.erase_erasable_artifacts!
end

流水线中设置过期时间

可以在定义ci流水线的yaml配置中添加关键字artifacts:expire_in

设置全局默认过期时间

默认是30天。

可以在 Main Menu > Admin >Settings >CI/CD > default expiration time 设置