1

I want to schedule a job to be at the top of the build queue in Jenkins via a Groovy Postbuild.

For example, for Build X

If previously the build queue looks like this:

  • Generic Build 1
  • Generic Build 2
  • Generic build 3

Now it should look like:

  • Build X
  • Generic Build 1
  • Generic Build 2
  • Generic Build 3

So far I've been able to schedule the job using

def waitingItem = Jenkins.get().getQueue().schedule(job, 0)

if (waitingItem == null) {
    manager.listener.logger.println "Error scheduling ${job}!"
} else {
    manager.listener.logger.println "${job.name} was scheduled!"
}

but now I also want waitingItem to be at the top of the queue.

Any help is much appreciated.

George Cimpoies
  • 884
  • 2
  • 14
  • 26

1 Answers1

2

Ok so after countless hours of browsing the web I've been able to come up with a Groovy Postbuild script that does the following: once it finishes the build, it triggers another build (let's call it buildToBeTriggered) and buildToBeTriggered automatically gets pushed at the front of the queue.

The code is below:

import hudson.model.AbstractProject
import hudson.model.Queue
import hudson.model.queue.QueueSorter
import jenkins.model.Jenkins

def job = (AbstractProject)Jenkins.get().getItem('gcimpoies-toTrigger')
def isSuccess = manager.getResult() == 'SUCCESS'
def isRelease = manager.getEnvVariable('RELEASE') == 'true'
def secondsToWait = 20

if (isSuccess) {

def waitingItem = Jenkins.get().getQueue().schedule(job, 0)
if (waitingItem == null) {
    manager.listener.logger.println "Error scheduling ${job}!"
} else {
    manager.listener.logger.println "${job.name} was scheduled!"
}
Thread.sleep(secondsToWait * 1000)

//replace the original queue sorter with one that will place our project build first in the queue
QueueSorter originalQueueSorter = Jenkins.get().getQueue().getSorter()

AcceleratedBuildNowSorter acceleratedBuildNowSorter = new AcceleratedBuildNowSorter(job, originalQueueSorter)
Jenkins.get().getQueue().setSorter(acceleratedBuildNowSorter);

// we sort the queue so that our project is next to be built on the list
Jenkins.get().getQueue().getSorter().sortBuildableItems(Jenkins.getInstance().getQueue().getBuildableItems())

}

class AcceleratedBuildNowSorter extends QueueSorter {

private final AbstractProject project
private final QueueSorter originalQueueSorter
private final AcceleratedBuildNowComparator comparator

AcceleratedBuildNowSorter(AbstractProject project, QueueSorter originalQueueSorter) {
    this.project = project
    this.originalQueueSorter = originalQueueSorter
    comparator = new AcceleratedBuildNowComparator(this.project)
}

@Override
void sortBuildableItems(List<Queue.BuildableItem> buildables) {
    if (this.originalQueueSorter != null) {
        this.originalQueueSorter.sortBuildableItems(buildables)
    }
    Collections.sort(buildables, comparator)
}
}

class AcceleratedBuildNowComparator implements Comparator<Queue.BuildableItem> {

private final AbstractProject mostPriorityProject;

AcceleratedBuildNowComparator(AbstractProject mostPriorityProject) {
    this.mostPriorityProject = mostPriorityProject;
}

int compare(Queue.BuildableItem buildableItem0, Queue.BuildableItem buildableItem1) {
    AbstractProject<?, ?> project0 = (AbstractProject<?, ?>) buildableItem0.task
    AbstractProject<?, ?> project1 = (AbstractProject<?, ?>) buildableItem1.task
    if (project0.equals(mostPriorityProject)) {
        return -1
    }
    if (project1.equals(mostPriorityProject)) {
        return 1
    }
    return 0
}
}
George Cimpoies
  • 884
  • 2
  • 14
  • 26