Jenkins delete builds older than latest 20 builds for all jobs

前端 未结 6 585
悲哀的现实
悲哀的现实 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:10

    This can be done in many ways. You can try the following

    1. get all your job names in a textfile by going to the jobs location in jenkins and run the following

    ls >jobs.txt

    Now you can write a shell script with a for loop

    #!/bin/bash
    ##read the jobs.txt
    for i in 'cat <pathtojobs.txt>'
         do
    curl -X POST http://jenkins-host.tld:8080/jenkins/job/$i/[1-9]*/doDeleteAll
         done
    

    the above deletes all the jobs

    you can also refer here for more answers

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-02 02:17

    There are lots of ways to do this

    Personally I would use the 'discard old builds' in the job config

    If you have lots of jobs you could use the CLI to step through all the jobs to add it

    Alternatively there is the configuration slicing plugin which will also do this for you on a large scale

    0 讨论(0)
  • 2021-01-02 02:17

    I had issues running the suggestions on my Jenkins instance. It could be because it is dockerized. In any case, removing the folder beforehand using the underlying bash interpreter fixes the issue. I also modified the script to keep 180 days of build logs and keep a minimum of 7 build logs:

    import jenkins.model.Jenkins
    import hudson.model.Job
    
    MIN_BUILD_LOGS = 7
    
    def sixMonthsAgo = new Date() - 180
    
    Jenkins.instance.getAllItems(Job.class).each { job ->
      println job.getFullDisplayName()
      
      def recent = job.builds.limit(MIN_BUILD_LOGS)
      
      def buildsToDelete = job.builds.findAll {
        !recent.contains(it) && ! (it.getTime() > sixMonthsAgo)
      }
      
      if (!buildsToDelete) {
        println "nothing to do"
      }
      for (build in buildsToDelete) {
        println "Preparing to delete: " + build + build.getTime()
        ["bash", "-c", "rm -r " + build.getRootDir()].execute()
        build.delete()
      }
    }
    
    "done"
    
    0 讨论(0)
  • 2021-01-02 02:27

    I got an issue No such property: builds for class: com.cloudbees.hudson.plugins.folder.Folder on Folders Plugin 6.6 while running @Dave Bacher's script

    Alter it to use functional api

    import jenkins.model.Jenkins
    import hudson.model.Job
    
    MAX_BUILDS = 5
    Jenkins.instance.getAllItems(Job.class).each { job ->
      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()
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-02 02:32

    For Multibranch Pipelines, I modified the script by Dave Bacher a bit. Use this to delete builds older than the latest 20 build of "master" branches:

    MAX_BUILDS = 20
    
    for (job in Jenkins.instance.items) {
      if(job instanceof jenkins.branch.MultiBranchProject) {
        job = job.getJob("master")
        def recent = job.builds.limit(MAX_BUILDS)
        for (build in job.builds) {
          if (!recent.contains(build)) {
            println "Preparing to delete: " + build
            // build.delete()
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题