Tuesday, April 12, 2022

Jenkins delete builds older than latest 20 builds for all jobs

 Ref: https://stackoverflow.com/questions/35610053/jenkins-delete-builds-older-than-latest-20-builds-for-all-jobs

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.

No comments:

Post a Comment

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...