Skip to content

Instantly share code, notes, and snippets.

@jdhom
Last active November 25, 2021 13:09
Show Gist options
  • Save jdhom/d610534f71be1755d38affe39d789233 to your computer and use it in GitHub Desktop.
Save jdhom/d610534f71be1755d38affe39d789233 to your computer and use it in GitHub Desktop.
jenkins console scripts

find job / publishers

import org.jenkinsci.plugins.github.*
import org.jenkinsci.plugins.github.status.err.*

def job = findJob(Hudson.instance.items, 'pb_jdhom_tech-ui')
println( job )

for (publisher in job.publishers) {
  println(publisher.value.errorHandlers)
  def handler = new ChangingBuildStatusErrorHandler('FAILURE')
  publisher.value.setErrorHandlers([handler])
  
}
job.save()

def findJob(items, jobName) {

  def result = null;
  
  for (job in items) {
  
    if (job.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {
  
      if (job.name == jobName) {
        println(job.name)
        result = job
      }
    }
  }
  
  return result
}

disable all personal builds

import hudson.model.*


disableChildren(Hudson.instance.items)

def disableChildren(items) {
  for (item in items) {
    if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder' 
        && item.name.startsWith('pb_') ) {
          
		item.disabled=true
		item.save()
		println(item.name)
    }
  }
}

disable all jobs

From jenkins-scripts

/*** BEGIN META {
      "name" : "disable all jobs",
      "comment" : "disables all jobs",
      "parameters" : [],
      "core": "1.300",
      "authors" : [
        { name : "Nicolas Mommaerts" }
      ]
    } END META**/
import hudson.model.*


disableChildren(Hudson.instance.items)

def disableChildren(items) {
  for (item in items) {
    if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {
	item.disabled=true
	item.save()
	println(item.name)
    } else {
	disableChildren(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())
    }
  }
}

running and queued jobs

//Jobs currently building or in the queue
jobs = hudson.model.Hudson.instance.items.findAll{ job -> job.isBuilding() } 
     + hudson.model.Hudson.instance.items.findAll{ job -> job.isInQueue() }

//Keep only the names from the collected job objects
jobs = jobs.collect{x -> x.name}

//get current job name
currentJob = Thread.currentThread().executable.toString().split()[0]

//cut current job from the jobs list
jobs -= currentJob

//decide if there is any running (or waiting) job
res = true
if ((jobs.size())  > 0)
{
    res = false
    println "The following jobs are being executed or waiting for an executor"
    jobs.each{println it}
}

return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment