For Jenkins using a Groovy System Script, is there a way to easily search the build queue and list of executing builds for some criteria (specifically a parameter that matches some condition) and then kill/cancel them?
I cannot seem to find any way to do this, but it seems like it should be possible.
I haven't tested it myself, but looking at the API it should be possible in the following way:
Relevant API links:
I know it's kind of an old question, but Google points me to this one. The scripts shown here only remove the jobs from the queue, and don't stop running builds. The following script, just removes everything from the queue and kills all running builds:
Referencie: https://xanderx.com/post/cancel-all-queued-jenkins-jobs/
Run this in Manage Jenkins > Script Console:
couldn't add as a comment, but as of today with latest jenkins, Andrey's script (nice) requires another import to work. Executing as system Groovy script.
Jenkins errors and mentions the missing class. I included the url that mentioned the issue:
Here is my solution, if you want to run only the newest job of same project from the build queue and cancel other:
Use the jenkins groovy postbuild plugin:
I think this would be the groovy script:
After some investigation, I came up with this code which works absolutely fine for me. It clears the queue and also aborts all the jobs currently getting executed.
Prerequisites:
To control Job build queue, you can use this Plugin also: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin
I've expanded upon the snippet by Igor Zilberman so that it also aborts running jobs when there is a job in the queue with the same cause (what you see when you hover over the job in the build queue, only looking at the first line). I’m running this as a job with build step “Execute System Groovy Script”.