Groovy script to get Jenkins pipeline's script path
Answer a question I want to run a script in Jenkins Script Console to retrieve scriptPath parameter of all jobs/pipelines configured in Jenkins. I found the way to get names of the pipelines but I wan
Answer a question
I want to run a script in Jenkins Script Console to retrieve scriptPath parameter of all jobs/pipelines configured in Jenkins. I found the way to get names of the pipelines but I want scriptPath parameters of each pipeline.
Any leads?
Answers
All pipeline jobs are instances of org.jenkinsci.plugins.workflow.job.WorkflowJob
and can be found using the Jenkins.instance.getAllItems
function.
Once found, each job contains an attribute of the FlowDefinition
class whcih is accessible via the getDefinition()
method. There are two types of definition for pipelines:
CpsFlowDefinition
- for pipelines which define an inline script (not SCM), the script is accessible via thegetScript()
method.CpsScmFlowDefinition
- for pipelines which define an SCM script, the script is accessible via thegetScriptPath()
method.
So to achieve what you want you can go over relevant jobs and extract the relevant attribute:
def pipelineJobs =Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)
def scmJobs = pipelineJobs.findAll { it.definition =~ 'CpsScmFlowDefinition'}
scmJobs.each {
println "Pipeline Name: ${it.name}"
println "SCM Script Path: ${it.definition.scriptPath}"
}
If all your jobs are SCM pipelines you can use the following single liner:
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)*.definition.scriptPath
For a single specific job you can use:
Jenkins.instance.getItemByFullName("<PIPELINE_NAME>").definition.scriptPath // or just script for inline definition
更多推荐
所有评论(0)