Jenkins delete builds older than latest 20 builds for all jobs

前端 未结 6 602
悲哀的现实
悲哀的现实 2021-01-02 01:30

I am in the process of cleaning up Jenkins (it was setup incorrectly) and I need to delete builds that are older than the latest 20 builds for every job.

Is there an

6条回答
  •  太阳男子
    2021-01-02 02:11

    You can use the Jenkins Script Console to iterate through all jobs, get a list of the N most recent and perform some action on the others.

    import jenkins.model.Jenkins
    import hudson.model.Job
    
    MAX_BUILDS = 20
    
    for (job in Jenkins.instance.items) {
      println job.name
    
      def recent = job.builds.limit(MAX_BUILDS)
    
      for (build in job.builds) {
        if (!recent.contains(build)) {
          println "Preparing to delete: " + build
          // build.delete()
        }
      }
    }
    

    The Jenkins Script Console is a great tool for administrative maintenance like this and there's often an existing script that does something similar to what you want.

提交回复
热议问题