Skip to content

Instantly share code, notes, and snippets.

@usmansaleem
Created August 16, 2018 23:28
Show Gist options
  • Save usmansaleem/5fcfb27d2f4cbf6427abc64e3bc2f2c4 to your computer and use it in GitHub Desktop.
Save usmansaleem/5fcfb27d2f4cbf6427abc64e3bc2f2c4 to your computer and use it in GitHub Desktop.
Cancel jenkins jobs except newest
https://stackoverflow.com/a/39635794
def q = Jenkins.instance.queue
//Find items in queue that match <project name>
def queue = q.items.findAll { it.task.name.startsWith('sample_project') }
//get all jobs id to list
def queue_list = []
queue.each { queue_list.add(it.getId()) }
//sort id's, remove last one - in order to keep the newest job, cancel the rest
queue_list.sort().take(queue_list.size() - 1).each { q.doCancelItem(it) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment