0

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

ycr
  • 12,828
  • 2
  • 25
  • 45
KNCK
  • 103
  • 2
  • 12

1 Answers1

1

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())
  }  
}
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Hi, Thanks for the help, I need jobs that reside in folders and views also and the total count. and also see that for Part 02 you have given same code, can you please check it once – KNCK Oct 28 '22 at 09:56
  • @KNCK the part02 is correct, if you take a closer look at the condition, the condition is negated(!). Regarding the Jobs in Views, since it was not mentioned in the original question please feel free to create a new SO ticket for that. – ycr Oct 28 '22 at 11:20
  • A job can only exist in one physical location: folder[/folder]/job. But it might appear in many views. – Ian W Oct 28 '22 at 20:18
  • how to get all enabled jobs using a groovy script, I have tried multiple ways, but no use, can anyone please give some script to only enabled jobs list – KNCK Nov 01 '22 at 12:34
  • @KNCK Isn't the above script working for you? – ycr Nov 01 '22 at 12:38
  • @ycr "List Jobs that are not disabled/active." I'm getting full list of jobs instead of only active pipelines – KNCK Nov 01 '22 at 16:00
  • @ycr Can you please let me know how to get active jenkins pipelines and exclude all multibranch jobs/branches – KNCK Apr 25 '23 at 13:43
  • @KNCK Please feel free to create new questions for new requirements. – ycr Apr 25 '23 at 17:18