Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovyjenkins-job-dsl

List all Jenkins Jobs including subfolders with count and list all enabled and disabled jenkins including subfolders with count using groovy script


In Jenkins, I want to list all Jenkins jobs using the groovy script, I have tried a couple of groovy scripts to get the jobs, and it's working fine, Now I want to get the count of all Jobs without folders and subfolders. Below is the groovy script which I have tried to the all jobs list including folder and subfolders, but I need only jobs which are WorkflowJob, FreestyleProject, and maven build

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

and 2 one is how can I get a list of active jobs and disabled jobs list and count I have tried from below reference, but it's not working

find disabled job link found in google

Someone please can help on this, help is much appreciated


Solution

  • Part 01

    I'm not sure what you meant by a Maven build Job. But, here is how to list all the Jobs which are either a Workflow Job or a Freestyle Job and which do not reside in a folder/subfolder.

    Jenkins.instance.getAllItems(Job.class).each { jobitem ->
          
      if(jobitem.getName() == jobitem.getFullName()) { // This means it's not in a folder
        if(jobitem instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob || jobitem instanceof hudson.model.FreeStyleProject) {
            println("Job Name: " + jobitem.getFullName())
        }
      }  
    }
    

    Part 02

    List disabled Jobs.

    Jenkins.instance.getAllItems(Job.class).each { jobitem ->     
      if(jobitem.isDisabled()) {
            println("Job Name: " + jobitem.getFullName())
      }  
    }
    

    List Jobs that are not disabled/active.

    Jenkins.instance.getAllItems(Job.class).each { jobitem ->     
      if(!jobitem.isDisabled()) {
            println("Job Name: " + jobitem.getFullName())
      }  
    }