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

15 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?

Please sign in to leave a comment.