Groovy Scripts To Shelve Jobs

Issue

A large number of jobs need to be shelved in your Jenkins environment.

Environment

  • CloudBees Jenkins Enterprise (CJE)

  • CloudBees Jenkins Operations Center (CJOC)

  • Jenkins

Resolution

Installing the Shelve Project Plugin on your Jenkins Master will allow you to manually shelve projects. However,
this can be a daunting task for environments with a large number of jobs.

To speed up the process, you can use the Script Console on your Jenkins Master or a cluster
operation on your CloudBees Jenkins Operations Center.

Please see the two groovy scripts below:

Groovy Script That Shelves Jobs Built X Days Ago

//You have to install the Shelve Project Plugin on your Jenkins Master
//The maximum value for daysBack is 365, going beyond 365 will break the script.

import org.jvnet.hudson.plugins.shelveproject.ShelveProjectTask

def daysBack=365; 
Jenkins.instance.getAllItems(AbstractProject.class).each{ it->
  def lastBuild=it.getLastBuild()
  if(lastBuild != null){
    def back = Calendar.getInstance()
    back.set(Calendar.DAY_OF_YEAR,back.get(Calendar.DAY_OF_YEAR)-daysBack)
    if (lastBuild.getTime().compareTo(back.getTime()) < 0) {
      println it.name + " was built over " + daysBack + " days ago: " + lastBuild.getTime()
      if (it instanceof AbstractProject){
      def spt=  new ShelveProjectTask(it)
        Hudson.getInstance().getQueue().schedule(spt , 0 );
      }else{
        println it.name + " was not shelved----------- " 
      }
    }
  }
}

Groovy Script That Shelves Jobs Built Over One Year Ago

//You have to install the Shelve Project Plugin On Your Jenkins Master

import org.jvnet.hudson.plugins.shelveproject.ShelveProjectTask
import jenkins.model.Jenkins


Jenkins.instance.getAllItems(AbstractProject.class).each{ it->
  def lastBuild=it.getLastSuccessfulBuild()
  if(lastBuild != null){
    def yearBack = new Date()
    yearBack.setYear(yearBack.getYear()-1);
    if (lastBuild.getTime().compareTo(yearBack) < 0) {
      println it.name + " was built over a year ago: " + lastBuild.getTime();
      if (it instanceof AbstractProject){
      def spt=  new ShelveProjectTask(it)
        Hudson.getInstance().getQueue().schedule(spt , 0 );
      }else{
        println it.name + " was not shelved----------- " 
      }
    }
  }
}

Running either of the above scripts in the Script Console (Manage Jenkins->Script Condole) will shelve the jobs it
finds.

Please note at the time of this writing, the most recent version of the Shelve Project Plugin, v1.5 does not
support pipeline jobs.

If you have several Jenkins Masters connected to your CJOC you can create a new cluster operation
to run the groovy script on each of the connected Jenkins Masters as shown in the screen shots below. The

Project Plugin
should be installed on each of the Jenkins Masters that the cluster operation will run on:

clusterOperationScreenShot.png

Put either of the groovy scripts from above in the script section.

Run the cluster operation. You will need to navigate to the console output for the build and click on log as shown in the screen shot below to see the output of the groovy script:

logScreenShot.png

Shelved jobs can be un-shelved as documented in Shelve Project Plugin wiki page.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.