Wednesday, September 25, 2019

How to cancel builds and clear queue jenkins

In the script console, run below script.

Reference



import jenkins.model.Jenkins

import  hudson.*

import hudson.model.*

import jenkins.*

//Remove everything which is currently queued

Jenkins.instance.queue.clear()

def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll

{

    it.isBuilding()

}


buildingJobs.each{


    def jobName = it.toString()

    def val = jobName.split("\\[|\\]")

    //'Abort jobs' is the name of the job I have created, and I do not want it to abort itself.

    if((val[1].trim())!='Abort jobs'){

        def job = Jenkins.instance.getItemByFullName(val[1].trim())

        for (build in job.builds) {

            if (build.isBuilding()){

                println(build)

                build.doStop();

            }


        }

    }

}

No comments:

Post a Comment