根据选项列表的数量和内容的变化,在 Jenkins 中动态生成多选选项列表

Dynamically generate a list for multi-choice options in Jenkins depending on the changing list of options both in number and content

我需要将必要的参数值传递给Jenkins,这些参数值由目录中的文件名组成。这些文件的数量和名称都可以改变,因此需要在与SCM 合并后形成它们。我会尝试使用用户输入参数,稍后我会描述发生了什么,但也许还有其他选择?谢谢!

我尝试通过用户输入参数。我设法添加了一个 drop-down 列表,我只能从中选择一个选项。但是我需要做什么才能select多个选项?

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)
}

我通过能够标记每个选项的复选框建议了可以做什么。为此,我尝试将列表中的每个值添加为单独的布尔参数。

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)
}

问题来了,如何根据它们的数量动态创建这些参数。我假设可以用我可以单独生成的代码的可变部分替换,具体取决于所需参数的数量

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)
    }

但是没用

 [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.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.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.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.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.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable.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[=13=]1(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.access0(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService.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

也许有人知道如何执行此操作的其他选项,或者可以告诉我如何正确格式化我的解决方案。非常感谢您的关注!

一切顺利,问题解决,语法问题 输入的变量值不带引号,即立即可执行

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]