Skip to content

Instantly share code, notes, and snippets.

@jimklimov
Last active April 19, 2025 13:23
Show Gist options
  • Save jimklimov/0c1b78ac040a0f874fa3065571a97cca to your computer and use it in GitHub Desktop.
Save jimklimov/0c1b78ac040a0f874fa3065571a97cca to your computer and use it in GitHub Desktop.
Mass-move Jenkins queue items
// requires Simple Queue Plugin : https://plugins.jenkins.io/simple-queue/
// run this in Jenkins console to generate shell code
// create a token in sysadmin account, save as e.g. `~/.sq` in a trusted workstation
// and in a shell session define JENKINS_URL="https://USERNAME:`cat ~/.sq`@SERVERNAME"
// then paste the result of script below (tweak "it" matcher to your needs)
def q = Jenkins.instance.queue
def pattern = ~/.*jobname.*1234.*/
def ACTION = "DOWN_FAST"
//def ACTION = "UP_FAST"
// NOTE: See plugin-simpleMove.groovy for a way to call the method from inside Jenkins
println "for ID in \\"
q.items.findAll { it.task.name ==~ pattern }.each { println it.id + " \\" }
println "; do ( curl -X POST \"\${JENKINS_URL}/simpleMove/move?moveType=${ACTION}&itemId=\${ID}\" || { sleep 120 ; curl -X POST \"\${JENKINS_URL}/simpleMove/move?moveType=${ACTION}&itemId=\${ID}\" ; } ) & done"
// requires Simple Queue Plugin : https://plugins.jenkins.io/simple-queue/
// run this in Jenkins console to perform the "prioritize" (or not) action right away
import cz.mendelu.xotradov.MoveActionWorker;
// https://github.com/jenkinsci/simple-queue-plugin/blob/master/src/main/java/cz/mendelu/xotradov/MoveActionWorker.java
def pattern = ~/.*jobname.*1234.*/
boolean prioritize = true
def mw = new MoveActionWorker()
def q = Jenkins.instance.queue
def ACTION = { Queue.Item item ->
if (prioritize) {
mw.moveToBottom(item, q) // "DOWN_FAST" for Item with greatest importance
} else {
mw.moveToTop(item, q) // "UP_FAST" for Item with least importance
}
}
// FIXME: Port code from plugin to not require it?
// ...and to prepare the sorted list ONCE and then apply it to Queue
def items = q.items.findAll { it.task.name ==~ pattern }
if (!prioritize)
items = items.reverse() // NOTE: reverse() to clear up the bottom-most part of the queue first
// Sometimes the action fails with
// java.lang.IllegalArgumentException: Comparison method violates its general contract!
// so we loop until our work is done
while (true) {
def items2 = []
println "======= Got ${items.size()} items to re-queue (" + (prioritize ? "" : "de-") + "prioritize)"
items.each {
println "* " + it //.id
try {
ACTION(it)
} catch (Throwable t) {
println "!!! ${it.id} got a problem: ${t}"
items2 << it
}
}
if (items2.size() > 0) {
println "======= RETRY earler misses"
items = items2
} else {
println "======= DONE"
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment