How to list the jenkins jobs script path programmatically How to list the jenkins jobs script path programmatically jenkins jenkins

How to list the jenkins jobs script path programmatically


I found a way to retrieve the path of the script. I've tested it for WorkflowJob. The following code prints the script itself if it's a local script, or the path if it's a remote one:

Jenkins.instance.getAllItems(Job) { job ->   if(job instanceof WorkflowJob) {       if(job.getDefinition() instanceof org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) {           println("Local script: ")           println(job.getDefinition().getScript())       }       else {           println("Remote script path: " + job.getDefinition().getScriptPath())       }   }}

For FreeStyleProject something like this can be done:

   import hudson.tasks.Shell   ...   if(job instanceof hudson.model.FreeStyleProject) {      println("Script: ")      job.builders.findAll { it in Shell }.collect { shell -> println(shell.command) }   }

I still haven't found a way for Multibranch pipelines, but since they are composed of WorkflowJobs, some extra effort might give the wanted result.

NOTE: Instead of using the getScriptPath() function, the property scriptPath can be used as well. Sometimes job.getDefinition() returns an object of type SCMBinder, which does not have the getScriptPath() method, but the property can be used there.