Child pages
  • Remove Git Plugin BuildsByBranch BuildData

Due to some maintenance issues, this service has been switched in read-only mode, you can find more information about the why

and how to migrate your plugin documentation in this blogpost

Skip to end of metadata
Go to start of metadata

This script is used to remove the static list of BuildsByBranch that is uselessly stored for each build by the Git Plugin.

  • This is a workaround for the problem described here: https://issues.jenkins-ci.org/browse/JENKINS-19022
  • Updated to handle Matrix Project types.
  • Updated to better support SCM Polling
  • Updated to handle Projects inside Folders.
  • Updated to handle Pipeline job types (just call getJobNames() to find everything)

import hudson.matrix.*
import hudson.model.*

hudsonInstance = hudson.model.Hudson.instance
jobNames = hudsonInstance.getJobNames()
allItems = []
for (name in jobNames) {
  allItems += hudsonInstance.getItemByFullName(name)
}
 
// Iterate over all jobs and find the ones that have a hudson.plugins.git.util.BuildData
// as an action.
//
// We then clean it by removing the useless array action.buildsByBranchName
//

for (job in allItems) {
  println("job: " + job.name);
  def counter = 0;
  for (build in job.getBuilds()) {
    // It is possible for a build to have multiple BuildData actions
    // since we can use the Mulitple SCM plugin.
    def gitActions = build.getActions(hudson.plugins.git.util.BuildData.class)
    if (gitActions != null) {
      for (action in gitActions) {
        action.buildsByBranchName = new HashMap<String, Build>();
        hudson.plugins.git.Revision r = action.getLastBuiltRevision();
        if (r != null) {
          for (branch in r.getBranches()) {
            action.buildsByBranchName.put(branch.getName(), action.lastBuild)
          }
        }
        build.actions.remove(action)
        build.actions.add(action)
        build.save();
        counter++;
      }
    }
    if (job instanceof MatrixProject) {
      def runcounter = 0;
      for (run in build.getRuns()) {
        gitActions = run.getActions(hudson.plugins.git.util.BuildData.class)
        if (gitActions != null) {
          for (action in gitActions) {
            action.buildsByBranchName = new HashMap<String, Build>();
            hudson.plugins.git.Revision r = action.getLastBuiltRevision();
            if (r != null) {
              for (branch in r.getBranches()) {
                action.buildsByBranchName.put(branch.getName(), action.lastBuild)
              }
            }
            run.actions.remove(action)
            run.actions.add(action)
            run.save();
            runcounter++;
          }
        }
      }
      if (runcounter > 0) {
        println(" -->> cleaned: " + runcounter + " runs");
      }
    }
  }
  if (counter > 0) {
    println("-- cleaned: " + counter + " builds");
  }
}
  • No labels

4 Comments

  1. Unknown User (david_perez)

    How do I run this script?

    I know is in the  Groovy language.

    Trying script console, but it takes a lot to run.

  2. Unknown User (jbochenski)

    I'm getting cryptic serialization errors, apparently related to Blueocean plugin when trying to run this. JENKINS-48941 - Getting issue details... STATUS

  3. Unknown User (grzzie)

    I'm gettit NullPointerException on multijob:

    job: FAT_00_MULTIJOB
    java.lang.NullPointerException
    Caused: com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call hudson.model.FreeStyleBuild.writeReplace() : null
    	at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.callWriteReplace(SerializationMethodInvoker.java:95)
  4. Unknown User (hsantana29)

    Is this script still relevant? We are still using it in our system, but we were considering retiring it.