Running variable list of jenkins jobs with a parameter in parallel

Viewed 94

I want to run a variable set of Jenkins jobs in parallel upon a run of one job. The master job takes a parameter I want to pass down on each job.

I have the following code for the pipeline, where TARGET_BRANCH is the param the parent takes and passes down to the children:

@NonCPS
def makeList(TARGET_BRANCH) {
    def targets = [:]
    hudson.model.Hudson.instance.getView('WSS').items.each {
        if (it.fullName.startsWith('wss-run')) {
            def fullName = it.fullName;
            targets['Switching '.concat(fullName)] = {
                build job: fullName, parameters: [string(name: 'TARGET_BRANCH', value: TARGET_BRANCH)]
            }
        }
    }
    return targets
}

pipeline {
    agent any
    stages {
        stage('Switch') {
            steps {
                script {
                    parallel makeList("${TARGET_BRANCH}")
                }
            }
        }
    }
}

And this sort of works, in that the child jobs do build... But the master job itself appears to hang, and shows its status as still running the first child job, even after all child jobs are done.

Also worth pointing out, the parameter in both the parent and child are defined as a persistent string and the console output shows

The parameter 'TARGET_BRANCH' did not have the type expected by {job_name}. Converting to Persistent String Parameter.

I'm not sure how to properly cast it in advance and if maybe this cast is the reason the main job fails to end. I don't really care about the cast warning though. I only care that the main job finishes when all child jobs finish (be they successful or failed).

I am using the latest Jenkins LTS version at this time (2.346.2) with all plugins up to date.

What am I doing wrong?

1 Answers

Try adding wait : true to the build job target, so then the main job gets an exit code from each downstream job, so it will know that the parallel stage has completed and can move to the next stage in the pipeline:

targets['Switching '.concat(fullName)] = {
    build job: fullName, 
        parameters: [
            string(name: 'TARGET_BRANCH', value: TARGET_BRANCH)
        ], 
        wait : true
}

Or on the flip-side, you can use wait : false if you want the main job to finish as soon as the downstream jobs have kicked off.

In regards to the parameter type, you could try to retrieve the class type of the parameter and pass that on to see if removes the warning, like so:

env.CLASS_TYPE=params.TARGET_BRANCH.getClass()
echo "${env.CLASS_TYPE}"

if the echo output is class java.lang.String then the build job target would be:

targets['Switching '.concat(fullName)] = {
    build job: fullName, 
        parameters: [
            string($class: 'java.lang.String', name: 'TARGET_BRANCH', value: TARGET_BRANCH)
        ], 
        wait : true
}
Related