Groovy to list all jobs

Issue

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

Environment

  • CloudBees Jenkins Enterprise
  • Jenkins

Resolution

If you go to the groovy script console run the command:

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

This will print the name of all the jobs including jobs inside of a folder.

Have more questions? Submit a request

11 Comments

  • 0
    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
Please sign in to leave a comment.