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

2 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
    }
    }

Please sign in to leave a comment.