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:

  1. CpsFlowDefinition - for pipelines which define an inline script (not SCM), the script is accessible via the getScript() method.
  2. CpsScmFlowDefinition - for pipelines which define an SCM script, the script is accessible via the getScriptPath() 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
Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐