如何使用Groovy动态构建Jenkins管道输入参数?

如何使用Groovy动态构建Jenkins管道输入参数?,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,在Jenkins管道中,可以使用 def returnValue = input message: 'Need some input', parameters: [string(defaultValue: 'adefval', description: 'a', name: 'aaa'), string(defaultValue: 'bdefval', description: 'b', name: 'bbb')] 为了动态构建这样一个列表,我尝试了以

在Jenkins管道中,可以使用

def returnValue = input message: 'Need some input', 
   parameters: [string(defaultValue: 'adefval', description: 'a', name: 'aaa'),
                string(defaultValue: 'bdefval', description: 'b', name: 'bbb')] 
为了动态构建这样一个列表,我尝试了以下方法

def list = ["foo","bar"]
def inputRequest = list.collect  { string(defaultValue: "def", description: 'description', name: it)  }

def returnValue = input message: 'Need some input', parameters: [inputRequest]   
这不起作用:

java.lang.ClassCastException:class org.jenkinsci.plugins.workflow.support.steps.input.InputStep.setParameters()需要类hudson.model.ParameterDefinition,但收到类java.util.ArrayList

Groovy可能可以在第一种情况下动态确定需要哪个对象,但在第二种情况下它不再工作了,因为collect返回ArrayList

如何正确创建这样的列表


编辑:也许这个问题本身不是很有用,但可能仍然可以作为代码示例…

好的,这是一个非常简单的修复,因为collect已经返回了一个
数组列表
,设置参数时不应该将它包装到另一个列表中

def returnValue = input message: 'Need some input', parameters: inputRequest  

您可以使用下面的代码。它将为您生成选项A、B、C。这可以通过使用Jenkins中的Pipeline语法选项找到。我使用它是因为它节省了时间和更少的打字错误

def createInputParameters(){
properties([[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: 
false],parameters([choice(choices: ['A', 'B', 'C'], description: '', name: 
'Test')]),
[$class: 'ThrottleJobProperty', categories: [],
limitOneJobWithMatchingParams: false,
maxConcurrentPerNode: 0, maxConcurrentTotal: 0,
 paramsToUseForLimit: '',
  throttleEnabled: false,
   throttleOption: 'project']])
}