Dynamic number of parallel steps in declarative pipeline

fredrik

I'm trying to create a declarative pipeline which does a number (configurable via parameter) jobs in parallel, but I'm having trouble with the parallel part.

Basically, for some reason the below pipeline generates the error

 Nothing to execute within stage "Testing" @ line .., column ..

and I cannot figure out why, or how to solve it.

import groovy.transform.Field
@Field def mayFinish = false

def getJob() {
    return {
        lock("finiteResource") {
            waitUntil {
                script {
                    mayFinish
                }
            }
        }
    }
}

def getFinalJob() {
    return {
        waitUntil {
            script {
                try {
                    echo "Start Job"
                    sleep 3 // Replace with something that might fail.
                    echo "Finished running"
                    mayFinish = true
                    true
                } catch (Exception e) {
                    echo e.toString()
                    echo "Failed :("
                }
            }
        }
    }
}

def getJobs(def NUM_JOBS) {
    def jobs = [:]
    for (int i = 0; i < (NUM_JOBS as Integer); i++) {
        jobs["job{i}"] = getJob()
    }
    jobs["finalJob"] = getFinalJob()
    return jobs
}

pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr:'5'))
    }
    parameters {
        string(
            name: "NUM_JOBS",
            description: "Set how many jobs to run in parallel"
        )
    }
    stages {
        stage('Setup') {
            steps {
                echo "Setting it up..."
            }
        }
        stage('Testing') {
            steps {
                parallel getJobs(params.NUM_JOBS)
            }
        }
    }
}

I've seen plenty of examples doing this in the old pipeline, but not declarative. Anyone know what I'm doing wrong?

Christopher Orr

At the moment, it doesn't seem possible to dynamically provide the parallel branches when using a Declarative Pipeline.

Even if you have a stage prior where, in a script block, you call getJobs() and add it to the binding, the same error message is thrown.

In this case you'd have to fall back to using a Scripted Pipeline.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dynamically defining parallel steps in declarative jenkins pipeline

Jenkins declarative pipeline parallel steps executors

Assigning variables in a parallel step using Declarative Pipeline steps in Jenkins

Jenkins parallel declarative pipeline

Parallel runs in declarative pipeline

parallel steps on different nodes on declarative jenkins pipeline cannot access variables defined in global scope

Jenkins Declarative Pipeline parallel stages

Parallel items in Jenkins Declarative pipeline

Dynamic Variable in Jenkins Declarative Pipeline

Jenkins dynamic declarative pipeline parameters

Parallel Example Jenkinsfile Declarative Pipeline Fails

Parallel checkout in declarative Jenkins pipeline on multiple nodes

Jenkinsfile Declarative Pipeline defining dynamic env vars

Declarative Pipeline with dynamic matrix axis values

Jenkins pipeline: execute steps in parallel dynamically

collect data from Jenkins pipeline parallel steps

Jenkins Pipeline and huge amount of parallel steps

Creating a sequential step in a jenkins declarative pipeline preceding a parallel stage

How to detect which parallel stage failed in a Jenkins declarative pipeline?

How to make parallel calls to the same function in stage in Jenkins Declarative Pipeline

How to restrict parallel jobs to particular slaves in Declarative Pipeline

How make dynamic change stage name in Jenkinsfile Declarative pipeline?

Jenkins Declarative Pipeline - Dynamic Environment Variable Based On Git Branch

Issue with stage's directory in parallel steps on Jenkins Pipeline

How to share environment variables in parallel Bitbucket pipeline steps?

Running non-matrix stages in parallel to a matrix in Jenkins declarative-pipeline

Dynamic Programming for the number of ways of climbing steps

Dynamic programming table for number of steps problem

Jenkins scripted pipeline or declarative pipeline