0

Groovy syntax to retrieve a job's type

I've seen and used the plugins to get the job types of a job on Jenkins but I'd like to write a groovy script to display all existing jobs and their type. I know that the plugins show some job types as unknown. The plugins have a way to grab the job type, is there a groovy way to do it?

6 comments

  • 1
    Avatar
    Manuel Cardenas

    Hi Brandon,

     

    You can run this script from the Jenkins script console. This is a very basic approach, but I am sure that you can refine it.

    ```

    Jenkins.instance.getAllItems(Job).each{job -> job.isBuildable()

    if (job.isBuildable()!=null)

    println job.name +"--->"+ job.class
    }

    ```

     

    Best regards,

  • 0
    Avatar
    Brandon Bhagwandeen

    Thanks @Manuel, that works amazingly.

     

    Now i'm trying to filter out certain classes so i can find out all the classes that exist in my results. For some reason my statement:

    if(!(job instanceof MavenModule))

    still does not filter out  class 'hudson.maven.MavenModule' results

  • 0
    Avatar
    Brandon Bhagwandeen

    I think it's because MavenModule is not a job, but a plugin?

  • 1
    Avatar
    Manuel Cardenas

    You will need to provide the FQN: hudson.maven.MavenModuleSet 

    That should work. If you review the output from the execution of the first script that would help you use the correct class name for the jobs that you want to work with.

     

    Hope that helps.

     

  • 0
    Avatar
    Brandon Bhagwandeen

    Thank you @Manuel. That works.

  • 0
    Avatar
    Brandon Bhagwandeen

    In case this question gets searched for again,

    job.getClass().simpleName

    This would show only the project class type name without the library information.

    For example, you would see this if anyone would need to: 

    test--->FreeStyleProject
    Edited by Brandon Bhagwandeen
Please sign in to leave a comment.