Groovy to list all jobs

Issue

  • Is there a way I can list all Jenkins jobs on a server?

Environment

Resolution

Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves:

  Jenkins.instance.getAllItems(AbstractItem.class).each {
  	println(it.fullName)
  };

This script will print the name of all jobs including jobs inside of a folder, but not the folders themselves.

  Jenkins.instance.getAllItems(Job.class).each{ 
  println it.name + " - " + it.class
  }

This script will recursively print the name of all jobs implementing the AbstractProject class, i.e. Freestyle and Maven jobs.

  Jenkins.instance.getAllItems(AbstractProject.class).each {it ->
    println it.fullName;
  }

Tested product/plugin versions

  • CloudBees Jenkins Enterprise 2.138.1.2
Have more questions? Submit a request

16 Comments

  • 1
    Avatar
    Muthukumar Muthukumar

     

    Jenkins.instance.getAllItems(AbstractProject.class).each {it ->
      println it.fullName;
    }

    The above script, did not give the complete list of jobs, I have tried the below command and it listed all jobs:

    import jenkins.model.*
    import hudson.model.*
    Jenkins.instance.getAllItems(AbstractItem.class).each { println(it.fullName) };

  • 0
    Avatar
    Илья Росман

    Because of it doesn't show jobs in folders

    There are 2 methods:

    1) Recursion

    import com.cloudbees.hudson.plugins.folder.*
    void processFolder(Item folder) {
    folder.getItems().each{
    if(it instanceof Folder){
    processFolder(it)
    }else{
    processJob(it)
    }
    }
    }
    void processJob(Item job){
    println job.name
    }

    Jenkins.instance.getItems().each{
    if(it instanceof Folder){
    processFolder(it)
    }else{
    processJob(it)
    }
    }

    2) Using getAllItems which is recursive by itself

    import hudson.matrix.*
    import jenkins.model.*;
    import com.cloudbees.hudson.plugins.folder.*
    Jenkins.getInstance().getAllItems().each {
    // MavenModule is superfluous project returned by getAllItems()
    if (!(it instanceof MavenModule || it instanceof MatrixConfiguration)) {
    println it
    }
    }

  • 0
    Avatar
    Maulik Shah

    Can you help if I want to fetch count for Number of Jobs which are running as specific time interval - For example After each our i will fetch values for Number of Jobs Running 

  • 0
    Avatar
    Lochlainn Wilkinson

    Is there a way to only display "Disabled" jobs?

  • 0
    Avatar
    Arnaud Heritier

    Hi

    Something like this should do the job

     

    jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll{it -> it.disabled}.each {it ->
    println it.fullName;
    }
  • 0
    Avatar
    Lochlainn Wilkinson

    HI Arnaud,

    I am getting an exception error:

    groovy.lang.MissingPropertyException: No such property: disabled for class: org.jenkinsci.plugins.workflow.job.WorkflowJob


    Any idea what is missing?
  • 0
    Avatar
    Manuel Cardenas

    Hi Lochlainn,

    Could you try isDisabled() instead ?

  • 0
    Avatar
    Lochlainn Wilkinson

    Hi Manuel,

     

    I'm not sure this is what you mean, I still get an exception error:

    jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll{it -> isDisabled()}.each {it ->
    println it.fullName;
    }

    groovy.lang.MissingMethodException: No signature of method: Script1.isDisabled() is applicable for argument types: () values: []

    Let me know if what I have is wrong?

    Thanks for the help!

  • 0
    Avatar
    Manuel Cardenas

    Could you try this simpler syntax?

     

     

    jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).each {

    if(it.isDisabled()){
    println it.fullName;
    }
    }
  • 0
    Avatar
    Manuel Cardenas

    Hi Lochlainn,

    You can also try this:

    jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll(){job -> job.isDisabled()}.each {


    println it.fullName;

    }
  • 0
    Avatar
    Lochlainn Wilkinson

    Hi Manuel,

    Seems as though the Disabled variable does not exist or is not loaded!

     

    groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.job.WorkflowJob.isDisabled() is applicable for argument types: () values: []
    Possible solutions: isBuildable(), isBuildable()

    If I change it to "isBuildable" it shows me what looks like all enabled jobs that are able to run. Do I need to load a module to get "isDisabled" to work?

    Cheeers,
    Lochlainn
  • 0
    Avatar
    Allan Burdajewicz

    Lochlainn,

    Support for disabling Pipeline jobs has been added in the Pipeline Job plugin version 2.11. So the method WorkflowJob#isDisabled() is only available since this version. Improvement captured as JENKINS-27299.

  • 0
    Avatar
    Lochlainn Wilkinson

    Thanks Allan,  good to know.

     

    We managed to get what we needed with the following:

    jenkins.model.Jenkins.instance.getAllItems(jenkins.model.ParameterizedJobMixIn.ParameterizedJob.class).findAll{job -> !(job.metaClass.methods*.name.findAll{method -> method == "isDisabled"}.isEmpty()) }.findAll{job -> job.disabled}.each{job ->

    def className = job.getClass();
    def lastBuild = job.getLastBuild()? job.getLastBuild().number: "Never built once";
    def lastBuildOnDate = job.getLastBuild()? job.getLastBuild().getTime():"Never built once"
    def url = job.getUrl()
    println "$url~$lastBuild~$lastBuildOnDate~$job.disabled"
    }

  • 0
    Avatar
    Ravikumar Vatti

    Hi Anyone,

     

    I need help on groovy. I'm looking for groovy code for stop/start Linux procedures using Jenkins. Here is my scenario, when I select "stop" and "start" choice parameters in Jenkins, respective(stop/start) shell  script or command should be invoked.

    Please let me know if you need more information.

    Appreciate your responses well in advance.

    Thanks!

     

     

  • 1
    Avatar
    Chris Lee

    Holy crap, could you update this article? The article doesn't work. but the comments all do. Maybe fill it with all the working code?

  • 0
    Avatar
    Anyone Noone

    Hi Everybody,

    My use case is similar to this topic so posting this here.

    we need to scan all jobs on server and see if they have SCM configured. If job exist with scm configured, Then print job URL & SCM URL. If job doesn't have scm, no need to print anything.

    I have below code to list of jobs :

    --------------------------------------

    Jenkins.instance.getAllItems(AbstractProject.class).each {it ->
      println it.fullName;
      }

    ----------------------------------------

    and i am using below code to list SCM URL :

    ----------------------------------------------

    Jenkins.instance.getAllItems(hudson.model.AbstractProject.class).each {it ->
      scm = it.getScm()
      if(scm instanceof hudson.plugins.git.GitSCM)
      {
        println scm.getUserRemoteConfigs()[0].getUrl()
      }
    }
    println "Done"

    ----------------------------------------------

    Above code is giving me both job & SCM  URL separately and i am not able to map what scm URL match to what job URL.

    Appreciate help !

Please sign in to leave a comment.