Want to trigger few downstream jobs in parallel is declarative pipeline using groovy. Tried as below but hitting the issue. It is checking the condition but seems to be hitting issue at the parallel block.
stage("CHeck for downstraem jobs"){
agent {
node {
label "${AGENT}"
customWorkspace "${SRC}"
}
}
environment {
UPSTREAM_PARAMS = "${SRC},${AGV}"
ADM_FILES = sh (script: 'cd ${SRC}/repo; git diff-tree --no-commit-id --name-only -r $(git log --format="%H" -n 1) | grep "src/util"',
returnStatus: true
)
FUNC_FILES = sh (script: 'cd ${SRC}/repo; git diff-tree --no-commit-id --name-only -r $(git log --format="%H" -n 1) | grep "include"',
returnStatus: true
)
CONT_FILES = sh (script: 'cd ${SRC}/repo; git diff-tree --no-commit-id --name-only -r $(git log --format="%H" -n 1) | grep "src/test"',
returnStatus: true
)
TOOL_FILES = sh (script: 'cd ${SRC}/repo; git diff-tree --no-commit-id --name-only -r $(git log --format="%H" -n 1) | grep "tools"',
returnStatus: true
)
}
steps{
echo "Build dir is ${SRC}"
echo "CHANGED FILES ARE ${ADMIN_FILES}"
echo "CHANGED FILES ARE ${FUNC_FILES}"
echo "CHANGED FILES ARE ${CONT_FILES}"
echo "CHANGED FILES ARE ${TOOL_FILES}"
script {
if (ADM_FILES == '0' && FUNC_FILES == '0' && CONT_FILES == '0') {
echo "trigger parallel both jobs"
parallel {
stage('Launch JOB admin'){
agent any
environment {
UPSTREAM_PARAMS = "${SRC},${AGV}"
}
steps {
build job: 'Downstream_admin_job', parameters: [string(name: 'UPSTREAM_PARAMS', value:"${UPSTREAM_PARAMS}")]
}
}
stage('Launch job sanity'){
agent any
environment {
UPSTREAM_PARAMS = "${SRC},${AGV}"
}
steps {
build job: 'Downstream_sanity_job', parameters: [string(name: 'UPSTREAM_PARAMS', value:"${UPSTREAM_PARAMS}")]
}
}
}
}
if (TOOL_FILES == '0' ) {
build job: 'Test_verification_job', parameters: [string(name: 'UPSTREAM_PARAMS', value:"${UPSTREAM_PARAMS}")]
}
}
}
}
But am not sure what is the issue here. Does starting parallel jobs inside the "script" block is not supported. Getting the below error.
java.lang.IllegalArgumentException: Expected named arguments but got org.jenkinsci.plugins.workflow.cps.CpsClosure2@6e72e37a
at org.jenkinsci.plugins.workflow.cps.DSL.singleParam(DSL.java:718)
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:706)
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:640)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:234)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at jdk.internal.reflect.GeneratedMethodAccessor26987.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:158)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:338)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at jdk.internal.reflect.GeneratedMethodAccessor303.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
Few things am not clear of. Can we start a parallel stages within "script" block again which is within "steps" block of another stage.
CodePudding user response:
The syntax you are using for the parallel command is the declarative syntax, but you are using it inside the script
block which requires scripted syntax and therefore the exception.
In your script
block you should use the scripted syntax for the parallel command:
parallel: Execute in parallel Takes a map from branch names to closures and an optional argument failFast > which will terminate all branches upon a failure in any other branch:
parallel firstBranch: { // do something }, secondBranch: { // do something else }, failFast: true|false
So in your case you can use something like the following with scripted syntax:
parallel(
'Launch JOB admin' : {
node {
build job: 'Downstream_admin_job', parameters: [string(name: 'UPSTREAM_PARAMS', value: "${SRC},${AGV}")]
}
},
'Launch job sanity': {
node {
build job: 'Downstream_sanity_job', parameters: [string(name: 'UPSTREAM_PARAMS', value: "${SRC},${AGV}")]
}
}
)
Alternatively you can try to avoid using the script
block by replacing your if
statements with a declarative when closure, and then you can use your current syntax.