Skip to main content

Groovy to list all jobs

Comments

19 comments

  • 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) };

    3
  • Илья Росман

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

    -1
  • 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
  • Lochlainn Wilkinson

    Is there a way to only display "Disabled" jobs?

    0
  • Arnaud Héritier

    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
  • 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
  • Manuel Cardenas

    Hi Lochlainn,

    Could you try isDisabled() instead ?

    0
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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?

    1
  • Anyone Noone

    Hi Everybody,

    My use case is similar to this topic so posting this here.

    we need to scan all jobs on server and see if they have SCM configured. If job exist with scm configured, Then print job URL & SCM URL. If job doesn't have scm, no need to print anything.

    I have below code to list of jobs :

    --------------------------------------

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

    ----------------------------------------

    and i am using below code to list SCM URL :

    ----------------------------------------------

    Jenkins.instance.getAllItems(hudson.model.AbstractProject.class).each {it ->
      scm = it.getScm()
      if(scm instanceof hudson.plugins.git.GitSCM)
      {
        println scm.getUserRemoteConfigs()[0].getUrl()
      }
    }
    println "Done"

    ----------------------------------------------

    Above code is giving me both job & SCM  URL separately and i am not able to map what scm URL match to what job URL.

    Appreciate help !

    0
  • Steven Christenson

    Anyone Noone:

            println (it.fullName + ": " + scm.getUserRemoteConfigs()[0].getUrl() )

    Instead of what you have... will show complete job path and the scmUrl.

    1
  • M K

    I am trying to list all our jenkins (Freestyle and Pipeline) job's git (URL, branch name) and perforce (port no, workspace name, view spec) details using below groovy, But for both freestyle and pipeline i could able to list the job names. Help needed to list SCM details for each job.

    import hudson.triggers.*
    import org.jenkinsci.plugins.workflow.job.*

    def logSpec = { it, getTrigger -> String spec = getTrigger(it)?.getSpec(); if (spec) println (it.getFullName() )}

    println("--- Jenkins Pipeline jobs List ---")
    Jenkins.getInstance().getAllItems(WorkflowJob.class).each() { logSpec(it, {it.getSCMTrigger()}) }

    println("\n--- Jenkins FreeStyle jobs List ---")
    Jenkins.getInstance().getAllItems(FreeStyleProject.class).each() { logSpec(it, {it.getSCMTrigger()}) }

    println '\nDone.'

    0
  • Zain Siddiqui

    @M K

    The above script is not listing out all the pipelines. I'm trying to get a list of all the pipelines, which includes multibranch pipelines and declarative pipelines. Any help would be much appreciated!

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.