1

I'm trying to list the jenkins jobs per plugin is being utilized. I see there's a plugin in jenkins, https://github.com/jenkinsci/plugin-usage-plugin, which displays the plugins usage in the Jenkins UI. But how do i get the same output from the groovy script?

2
  • i've a jenkins master and i'm trying list the jobs or builds that are being utilized by a particular plugin. Ex: how many jobs are configured/used by maven plugin
    – Sachin
    Commented Jan 26, 2021 at 21:31
  • i was trying to test these scripts, support.cloudbees.com/hc/en-us/articles/216351528. In a similar way i want to list all the jobs configured with a particular plugin.
    – Sachin
    Commented Jan 26, 2021 at 21:31

1 Answer 1

1

They quick and dirty answer is to "Display jobs group by the build steps they use" as updated below.

import hudson.model.*
import hudson.tasks.*

//All the projects on which we can apply the getBuilders method
def allProjects = Jenkins.instance.allItems.findAll{ it instanceof Project }

//All the registered build steps in the current Jenkins Instance
def allSteps = Builder.all()

//Group the projects by the build steps used
def projectsGroupBySteps = allSteps.inject([:]){
   map, step ->
   map[step.clazz.name] = allProjects.findAll{it.builders.any{ 
        it.class.name.contains(step.clazz.name)}}.collect{it.fullName}
   map
}

//Presentation
projectsGroupBySteps.sort().each{
    println "--- ($it.value.size) $it.key ---"
}
println ''

println 'Occcurences:'
projectsGroupBySteps.sort().each{
println "--- ($it.value.size) $it.key ---"
    it.value.each {
        println it }
    println ''
}
return

Note that a job has multiple sections: builders, publishers and buildWrappers. Depending on what you are looking for, replace

[allSteps = Builder.all(), allProjects.findAll{it.builders.any ]
[allSteps = Publisher.all(), allProjects.findAll{it.publishers.any ]
[allSteps = BuildWrapper.all(), allProjects.findAll{it.buildWrappers.any ]

Plugins may also be involved in the SCM section and as properties (eg: hudson.plugins.buildblocker.BuildBlockerProperty, com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty)

Obviously, same limitations of plugin-usage apply: does not scan pipeline steps plugin usage or build steps nested within an org.jenkinsci.plugins.conditionalbuildstep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.