Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all pipeline Jobs in Jenkins Groovy Script

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

Within this job I build the names of the other pipelines, I want to trigger from a list, given back from a 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, it might be the case, that a couple of jobs with the names I get from the Stage Get_Clusters_to_Build do not exist. Therefore they cannot be triggered and my job fails.

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

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

I want to do something like this in the 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
like image 228
Igle Avatar asked Dec 22 '17 11:12

Igle


2 Answers

try this syntax to get standard and pipeline jobs:

def jobs = Hudson.instance.getAllItems(hudson.model.Job.class)
like image 149
daggett Avatar answered Nov 04 '22 03:11

daggett


All pipeline jobs are instantces of org.jenkinsci.plugins.workflow.job.WorkflowJob. So you can get names of all Pipeline jobs using the following function

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

Then you can use it this way

//...
def jobs = getPipelineJobNames()
if (job_to_build in jobs) {
    //....
}
like image 34
Vitalii Vitrenko Avatar answered Nov 04 '22 04:11

Vitalii Vitrenko