gitlab artifacts过期时间查看和清理

artifacts过期时间查看

  • artifacts的默认过期时间是30天,可以通过gitlab UI界面查看

Main menu > Admin** > Settings > CI/CD > Continuous Integration and Deployment > Default artifacts expiration

  • 在gitlab CI pipeline中也可以通过expire_in参数来设置artifacts的过期时间

  • 在数据库中也可以查看artifacts的过期时间

    select project_id,size,job_id,created_at,updated_at,expire_at from ci_job_artifacts where project_id=xxx;

通过rails 控制台操作artifacts文件

  • 查看未过期的artifacts(实例级别)

    builds_with_artifacts_that_never_expire = Ci::Build.with_downloadable_artifacts.where(artifacts_expire_at: nil).limit(50)
    builds_with_artifacts_that_never_expire.find_each do |build|
      puts "Build with id #{build.id} has artifacts that don't expire and belongs to project #{build.project.full_path}"
    end
    
  • 查找从今天起7天后过期的构件的构建和项目

    builds_with_artifacts_that_expire_in_a_week = Ci::Build.with_downloadable_artifacts.where('artifacts_expire_at > ?', 7.days.from_now).limit(50)
    builds_with_artifacts_that_expire_in_a_week.find_each do |build|
      puts "Build with id #{build.id} has artifacts that expire at #{build.artifacts_expire_at} and belongs to project #{build.project.full_path}"
    end
    
  • 查看artifacts文件大小的前20个项目

    include ActionView::Helpers::NumberHelper
    ProjectStatistics.order(build_artifacts_size: :desc).limit(20).each do |s|
      puts "#{number_to_human_size(s.build_artifacts_size)} \t #{s.project.full_path}"
    end
    
  • 查看单个项目的前50个artifacts文件大小

    include ActionView::Helpers::NumberHelper
    project = Project.find_by_full_path('ops/test-2')
    Ci::JobArtifact.where(project: project).order(size: :desc).limit(50).map { |a| puts "ID: #{a.id} - #{a.file_type}: #{number_to_human_size(a.size)}" }
    
  • 查看单个项目的前5个artifacts文件信息

    p = Project.find_by_id(2)
    arts = Ci::JobArtifact.where(project: p)
    
    list = arts.order(size: :desc).limit(5).each do |art|
        puts "Job ID: #{art.job_id} - Size: #{art.size}b - Type: #{art.file_type} - Created: #{art.created_at} - File loc: #{art.file}"
    end
    
  • 删除早于特定日期的作业artifacts(保留job_log)(整个实例)

    # 清理1天以内的artifacts
    project = Project.find_by_full_path('ops/test-3')
    builds_with_artifacts =  project.builds.with_downloadable_artifacts
    
    builds_to_clear = builds_with_artifacts.where("finished_at > ?", 1.days.ago)
    builds_to_clear.find_each do |build|
      Ci::JobArtifacts::DeleteService.new(build).execute
      build.update!(artifacts_expire_at: Time.now)
    end
    
    
    # 清理10分钟以内的artifacts
    project = Project.find_by_full_path('ops/test-3')
    builds_with_artifacts =  project.builds.with_downloadable_artifacts
    
    builds_to_clear = builds_with_artifacts.where("finished_at > ?", 20.minutes.ago)
    builds_to_clear.find_each do |build|
      print "Ci::Build ID #{build.id}... "
      Ci::JobArtifacts::DeleteService.new(build).execute
      build.update!(artifacts_expire_at: Time.now)
    end
    
  • 删除早于特定日期的作业artifacts(保留job_log)(整个项目)

    # 清理1天以内的artifacts
    project = Project.find_by_full_path('ops/test-3')
    builds_with_artifacts = Ci::Build.with_downloadable_artifacts
    
    builds_to_clear = builds_with_artifacts.where("finished_at < ?", 2.days.ago)
    builds_to_clear.find_each do |build|
      print "Ci::Build ID #{build.id}... "
      Ci::JobArtifacts::DeleteService.new(build).execute
      build.update!(artifacts_expire_at: Time.now)
    end
    
  • 从特定日期前完成的作业中删除作业artifacts和日志(整个实例)

    # 清理1小时以内的artifacts
    builds_with_artifacts = Ci::Build.with_existing_job_artifacts(Ci::JobArtifact.trace)
    admin_user = User.find_by(username: 'root')
    
    builds_to_clear = builds_with_artifacts.where("finished_at > ?", 1.hours.ago)
    builds_to_clear.find_each do |build|
      print "Ci::Build ID #{build.id}... "
    
      if build.erasable?
        Ci::BuildEraseService.new(build, admin_user).execute
        puts "Erased"
      else
        puts "Skipped (Nothing to erase or not erasable)"
      end
    end
    
  • 从特定日期前完成的作业中删除作业artifacts和日志(整个项目)

    # 清理5小时之内的artifacts
    project = Project.find_by_full_path('ops/test-2')
    builds_with_artifacts =  project.builds.with_existing_job_artifacts(Ci::JobArtifact.trace)
    admin_user = User.find_by(username: 'root')
    
    builds_to_clear = builds_with_artifacts.where("finished_at > ?", 10.minutes.ago) 
    builds_to_clear.find_each do |build|
      print "Ci::Build ID #{build.id}... "
    
      if build.erasable?
        Ci::BuildEraseService.new(build, admin_user).execute
        puts "Erased"
      else
        puts "Skipped (Nothing to erase or not erasable)"
      end
    end
    
  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GitLab中,`artifacts`关键字用于定义构建产物(Artifacts),即构建过程中生成的文件或目录。构建产物可以在构建完成后被保存,并且可以在后续的构建或作业中使用或下载。 下面是一个示例的`.gitlab-ci.yml`文件,展示了`artifacts`关键字的使用: ```yaml stages: - build build_job: stage: build script: - echo "Building..." - # 执行构建命令,生成构建产物 - make build artifacts: paths: - bin/ # 将bin目录下的所有文件作为构建产物 expire_in: 1 week # 构建产物的过期时间设置为1周 ``` 在上述示例中,我们定义了一个名为`build_job`的作业,它属于`build`阶段。作业中的脚本会执行构建命令,并生成构建产物。 通过`artifacts`关键字,我们可以指定需要保存的构建产物。在示例中,我们指定了`bin/`路径,表示将`bin`目录下的所有文件作为构建产物保存起来。您可以根据实际需求指定多个路径或者单个文件。 此外,我们还可以通过`expire_in`字段设置构建产物的过期时间。在示例中,我们将过期时间设置为1周,意味着构建产物会在1周后被自动清理。 保存的构建产物可以在GitLab的作业页面中进行查看和下载。其他作业可以通过依赖关系来使用这些构建产物,例如在后续的测试或部署作业中使用构建产物。 希望这个示例对您有所帮助。如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值