Get all piping work in Jenkins Groovy Script

I want to run several different jobs on the pipeline, depending on the input parameters of the Controller Pipeline job.

In this task, I am building the names of other pipelines, I want to call from the list returned from the python script.

node {
    stage('Get_Clusters_to_Build') {
        copyArtifacts filter: params.file_name_var_mapping, fingerprintArtifacts: true, projectName: 'UpdateConfig', selector: lastSuccessful()
        script {
            cmd_string = 'determine_ci_builds --jobname ' + env.JOB_NAME
            clusters = bat(script: cmd_string, returnStdout: true)
            output_array = clusters.split('\n')
            cluster_array = output_array[2].split(',')
        }
        echo "${clusters}"
    }

    jobs = Hudson.instance.getAllItems(AbstractProject.class)

    echo "$jobs"
    def builders = [:]
    for (i=0; i<cluster_array.size(); i++) {
        def cluster = cluster_array[i]
        def job_to_build = "BuildCI_${cluster}".trim()
        echo "### branch${i}"
        echo "### ${job_to_build}"
        builders["${job_to_build}"] =
        {
            stage("${job_to_build}") {
                build "${job_to_build}"
            }
        }
    }
    parallel builders

    stage ("TriggerTests") {
        echo "Done"   
    }
}

My problem is that it may be that a few jobs with the names that I get from Stage Get_Clusters_to_Builddo not exist. Therefore, they cannot be started, and my work failed.

Now, to my question, is there a way to get the names of all the pipeline jobs and how to use them to check if I can invoke the assembly?

I tried jobs = Hudson.instance.getAllItems(AbstractProject.class), but it only gives me the "normal" FreeStyleProject-Jobs.

I want to do something like this in a loop:

def builders = [:]
for (i=0; i<cluster_array.size(); i++) {
    def cluster = cluster_array[i]
    def job_to_build = "BuildCI_${cluster}".trim()
    echo "### branch${i}"
    echo "### ${job_to_build}"

    // This part I only want to be executed if job_to_build is found in the jobs list, somehow like:
    if job_to_build in jobs: // I know, this is not proper groovy syntax
        builders["${job_to_build}"] =
        {
            stage("${job_to_build}") {
                build "${job_to_build}"
            }
        }
}
parallel builders
+6
3

org.jenkinsci.plugins.workflow.job.WorkflowJob. , Pipeline,

@NonCPS
def getPipelineJobNames() {
    Hudson.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)*.fullName 
}

//...
def jobs = getPipelineJobNames()
if (job_to_build in jobs) {
    //....
}
+10

@Vitalii Vitrenko,

for (job in Hudson.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)) {
    println job.fullName
}
+3

try this syntax for standard and pipelined jobs:

def jobs = Hudson.instance.getAllItems(hudson.model.Job.class)
+2
source

Source: https://habr.com/ru/post/1691229/


All Articles