Home > Blockchain >  Dynamically generate a list for multi-choice options in Jenkins depending on the changing list of op
Dynamically generate a list for multi-choice options in Jenkins depending on the changing list of op

Time:03-27

I need to pass the necessary parameter values ​​to Jenkins, which are formed from the names of the files in the directory. The number of these files, as well as the names can change, so it is necessary to form them after they merge with the SCM. I'll try to use User Input parameters, I'll describe what happened later, but maybe there are other options? Thanks!

CodePudding user response:

I tried via User Input parameters. I managed to add a drop-down list from which I can choose only one option. But what do I need to do to be able to select multiple options?

enter image description here

properties(
    [
        parameters(
            [
                choice(
                    name: 'SomeParam',
                    choices: ['some', 'any'], 
                    description: 'some, any' 
                )
            ]
        )
    ]
)
def UserInput(String SomeParam)
{
    stage('User Input')
    {
        //worked01
        def USER_INPUT = 
        input message: 'Parameters:', ok: 'Start',
        parameters: [
          [$class: 'ChoiceParameterDefinition',
          choices: ['foo', 'bar'],
          name: 'SomeChoices',
          description: 'Menu - select options']
        ]
        echo "USER_INPUT=${USER_INPUT}"
        return USER_INPUT   
    }
}
node{
    UserInput(SomeParam)
}

enter image description here

I suggested what can be done through the check box with the ability to mark each option. To do this, I tried to add each value from the list as a separate boolean parameter.

enter image description here

properties(
    [
        parameters(
            [
                choice(
                    name: 'SomeParam',
                    choices: ['some', 'any'], 
                    description: 'some, any' 
                )
            ]
        )
    ]
)
def UserInput(String SomeParam)
{
    stage('User Input')
    {
        def USER_INPUT = input message: 'User Input', ok: 'Start', parameters: [booleanParam(description: 'its foo', name: 'foo'), booleanParam(description: 'its bar', name: 'bar')]
        echo "USER_INPUT=${USER_INPUT}"
        return USER_INPUT   
    }
}
node{
    UserInput(SomeParam)
}

enter image description here

The question arises, how to dynamically create these parameters from their number. I assumed that it is possible to replace with a variable part of the code that I can generate separately, depending on the number of required parameters

properties(
        [
            parameters(
                [
                    choice(
                        name: 'SomeParam',
                        choices: ['some', 'any'], 
                        description: 'some, any' 
                    )
                ]
            )
        ]
    )
    def UserInput(String SomeParam)
    {
        stage('User Input')
        {
            env.var01 = "booleanParam(description: 'its 1', name: '1')"
            env.var02 = "booleanParam(description: 'its 2', name: '2')"
            def USER_INPUT = input message: 'User Input', ok: 'Start', parameters: ['$var01', '$var02']
            echo "USER_INPUT=${USER_INPUT}"
            return USER_INPUT   
        }
    }
    node{
        UserInput(SomeParam)
    }

But it didn't work

 [Pipeline] stage
 [Pipeline] { (User Input)
 [Pipeline] input
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] }
 [Pipeline] // node
 [Pipeline] End of Pipeline
 java.lang.ClassCastException: class org.jenkinsci.plugins.workflow.support.steps.input.InputStep.setParameters() expects class hudson.model.ParameterDefinition but received class java.lang.String
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerceList(DescribableModel.java:585)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:458)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:429)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:331)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:302)
    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.GeneratedMethodAccessor88.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 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
 Caused: java.lang.IllegalArgumentException: Could not instantiate {message=User Input, ok=Start, parameters=[$var01, $var02]} for org.jenkinsci.plugins.workflow.support.steps.input.InputStep
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:302)
    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.GeneratedMethodAccessor88.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 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.UserInput(WorkflowScript:54)
    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.GeneratedMethodAccessor84.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.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
    at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
    at jdk.internal.reflect.GeneratedMethodAccessor86.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.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
    at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
    at jdk.internal.reflect.GeneratedMethodAccessor86.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.ConstantBlock.eval(ConstantBlock.java:21)
    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)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
 Finished: FAILURE

Maybe someone knows other options for how to do this, or can tell me how to properly format my solution. Thank you very much for your attention!

everything worked out, problem solved, syntax issue variable values ​​were entered without quotes, i.e. immediately made executable

def var01 = booleanParam(description: 'its 1', name: '1')
def var02 = booleanParam(description: 'its 2', name: '2')
def USER_INPUT = input message: 'User Input', ok: 'Start', parameters: [var01, var02]
  • Related