N. Blore
N. Blore

Reputation: 43

IllegalArgumentException: Expected named arguments (Jenkins)

I am getting the following error when trying to run my jenkins job. Any help would be much appreciated

java.lang.IllegalArgumentException: Expected named arguments but got [org.jenkinsci.plugins.workflow.cps.CpsClosure2@33c7c4a6, org.jenkinsci.plugins.workflow.cps.CpsClosure2@79505a8c, org.jenkinsci.plugins.workflow.cps.CpsClosure2@6a96df3, org.jenkinsci.plugins.workflow.cps.CpsClosure2@1a0cb771, org.jenkinsci.plugins.workflow.cps.CpsClosure2@17e3a262] at org.jenkinsci.plugins.workflow.cps.DSL.singleParam(DSL.java:606) at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:594) at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:534) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:219) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:178) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122) at sun.reflect.GeneratedMethodAccessor102.invoke(Unknown Source)

My code is:

node("dvsacvsmgmt") {

    stage("Build") {

        def buildJobs = []
        for (BACKEND_SERVICE in BACKEND_SERVICES) {
            SVC = BACKEND_SERVICE.replaceAll('-','_')

            switch (BRANCH_SVC) {
                case ["develop","master"]:
                    def buildJob = {
                        build "${ROOT_FOLDER}/2_Build/Backend/${SVC}/job_build_backend_" + BRANCH_SVC + "_" + SVC +".groovy"
                    }
                    buildJobs.add(buildJob)
                break
                default:
                    def buildJob = {
                        build "job_${SVC}": "${ROOT_FOLDER}/2_Build/Backend/${SVC}/job_build_backend_" + BRANCH_SVC + "_" + SVC +".groovy",
                                parameters: [gitParameter(name: "BRANCH", value: BRANCH_SVC)]
                    }
                    buildJobs.add(buildJob)
                break
            }
        }
        parallel(buildJobs)
    }
}

NOTE: My variables are defined at the top,

BRANCH, BRANCH_SVC, ROOT_FOLDER, BACKEND_SERVICES

Upvotes: 2

Views: 11482

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42264

You see this exception because buildJobs in your example is a list of closures and it should be a map instead. It would make sense to use backend service name as a key for the map you pass to parallel() method. Consider the following changes to your code:

node("dvsacvsmgmt") {
    stage("Build") {

        def buildJobs = [:]
        for (BACKEND_SERVICE in BACKEND_SERVICES) {
            SVC = BACKEND_SERVICE.replaceAll('-','_')

            switch (BRANCH_SVC) {
                case ["develop","master"]:
                    def buildJob = {
                        build "${ROOT_FOLDER}/2_Build/Backend/${SVC}/job_build_backend_" + BRANCH_SVC + "_" + SVC +".groovy"
                    }
                    buildJobs.put(BACKEND_SERVICE, buildJob)
                break
                default:
                    def buildJob = {
                        build "job_${SVC}": "${ROOT_FOLDER}/2_Build/Backend/${SVC}/job_build_backend_" + BRANCH_SVC + "_" + SVC +".groovy",
                                parameters: [gitParameter(name: "BRANCH", value: BRANCH_SVC)]
                    }
                    buildJobs.put(BACKEND_SERVICE, buildJob)
                break
            }
        }
        parallel(buildJobs)
    }
}

It invokes

buildJobs.put(BACKEND_SERVICE, buildJob)

instead

buildJobs.add(buildJob)

to create a map that is seen as named arguments in parallel method call.

Upvotes: 3

Related Questions