Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel queued builds and aborting executing builds using Groovy for Jenkins

Tags:

jenkins

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.

like image 478
Leslie Chong Avatar asked Sep 06 '12 17:09

Leslie Chong


People also ask

How do I abort all builds in Jenkins?

The builds in the pending queue can be easily cancelled from the Jenkins UI by manually clicking on the red "x" icon next to each build.

Where is Jenkins script console?

This feature can be accessed from "Manage Jenkins" > "Script Console". Or by visiting the sub-URL /script on your Jenkins instance.

What is quiet mode in Jenkins?

Rather than stopping Jenkins, you can put it into "Quiet Down" mode, which prevents any new builds from taking place. You can enable this via the URLs /quietDown and /cancelQuietDown , or via the CLI commands [cancel-]quiet-down . – Christopher Orr.


1 Answers

I haven't tested it myself, but looking at the API it should be possible in the following way:

import hudson.model.* import jenkins.model.Jenkins  def q = Jenkins.instance.queue  q.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) } 

Relevant API links:

  • http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html
  • http://javadoc.jenkins-ci.org/hudson/model/Queue.html
like image 74
Andrey Adamovich Avatar answered Nov 16 '22 13:11

Andrey Adamovich