Child pages
  • Remove Git Plugin BuildsByBranch BuildData
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.


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

hudsonInstance = hudson.model.Hudson.instance
allItems = hudsonInstance.getAllItems(AbstractProject.class);

// 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");
  }
}

3 Comments

  1. How do I run this script?

    I know is in the  Groovy language.

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

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

  3. 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)
Write a comment…