Jenkins管道:多选环境与调试级别同屏输入的取值问题
问题解决:Jenkins Pipeline合并多选与单选输入框并获取参数值
问题背景
原本Jenkins Pipeline中使用choice参数实现单选环境选择,改为多选后通过ExtendedChoiceParameterDefinition实现,但需要分两次弹出输入框。现在希望将多选环境参数与debuglevel单选参数放在同一输入屏中,却不知道如何获取多选参数Envs的值。
解决方案
当多个参数(包括ExtendedChoiceParameterDefinition的多选参数和普通choice参数)同时放在同一个input步骤中时,返回的userInput是一个键值对Map,其中键对应参数定义时的name属性。因此可以直接通过参数名称来获取对应的值,再对多选结果按分隔符拆分即可。
完整代码示例
if (isStartedByUser) { try { timeout(time: 60, unit: 'SECONDS') { // 定义多选环境参数 def multiSelect = new ExtendedChoiceParameterDefinition( "Envs", "PT_MULTI_SELECT", tf.join(","), "project name", "", "", "", "", "", "", "", "", "", "", "", "", "", tf.join(","), "", "", "", "", "", "", "", "", false, false, 15, "Available Environments", "," ) // 合并多选与单选参数到同一个输入框 def userInput = input( id: 'userInput', message: 'Please Select terraform params', parameters: [ multiSelect, choice( name: 'debuglevel', choices: "\nINFO\nTRACE\nDEBUG\nWARN\nERROR", description: 'Choose debug level' ) ] ) // 获取多选环境值并拆分 terraformdir = userInput['Envs'].split(',') println "Selected environments: ${terraformdir}" // 获取单选debug级别 env.TF_LOG = userInput['debuglevel'] } } catch (Exception e) { // 处理超时或用户取消的情况 println "Input canceled or timed out: ${e.getMessage()}" } }
关键说明
userInput['Envs']:直接通过多选参数的名称Envs获取选中的字符串(多个选项用逗号分隔)。.split(','):将逗号分隔的字符串拆分为环境列表,和你之前分开输入时的处理逻辑一致。userInput['debuglevel']:获取单选的debug级别,和原来的方式完全相同。
内容的提问来源于stack exchange,提问作者rismoney
相关产品推荐
相关产品推荐

