Jenkins Active Choices插件问题:复选框返回布尔值而非实际值
问题
使用Jenkins Active Choices插件在流水线中创建动态参数时,遇到复选框返回值为布尔值(true/false)而非指定实际值的问题。
相关Jenkinsfile代码片段
[$class: 'ChoiceParameter', choiceType: 'PT_RADIO', description: 'Select the Testing Mechanism', filterLength: 1, filterable: false, name: 'TESTING_METHOD', script: [ $class: 'GroovyScript', fallbackScript: [ classpath: [], sandbox: false, script: "return['Could not get the testing method']" ], script: [ classpath: [], sandbox: false, script: "return['Files','Tags']" ] ] ], [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML', omitValueField: true, description: 'Select the test suit you want to run', name: 'TAGS', referencedParameters: 'TESTING_METHOD', script: [ $class: 'GroovyScript', fallbackScript: [ classpath: [], sandbox: false, script: "return['Could not get The environments']" ], script: [ classpath: [], sandbox: false, script: """ if (TESTING_METHOD.equals('Tags')) { return '<form>' + '<input name="value" type="checkbox" value="fast"> Fast<br>' + '<input name="value" type="checkbox" value="slow"> Slow<br>' + '<input name="value" type="checkbox" value="Medium"> Medium<br>' + '<input name="value" type="checkbox" value="Rapid"> Rapid<br>' + '</form>' } else { return '<input name="value" type="text" value="Intel Core i5" disabled>' } """ ] ] ],
问题详情
- 当前行为:选择复选框后,返回值为布尔值(选中为true,未选中为false)
- 预期行为:选中的复选框应返回对应的实际值(fast、slow、Medium、Rapid)
解决方案
问题根源在于多个复选框共用name="value"时,Jenkins会默认返回每个复选框的选中状态(布尔值),而非实际的value属性内容。需修改HTML生成逻辑,让选中值能正确拼接后传递给Jenkins:
修改步骤
- 给复选框设置数组格式的
name属性(value[]),让浏览器识别为多值输入 - 添加隐藏字段和JavaScript逻辑,在表单提交时收集所有选中的复选框值,拼接为逗号分隔的字符串后赋值给隐藏字段,确保Jenkins能获取到实际值
修改后的代码片段
[$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML', omitValueField: true, description: 'Select the test suit you want to run', name: 'TAGS', referencedParameters: 'TESTING_METHOD', script: [ $class: 'GroovyScript', fallbackScript: [ classpath: [], sandbox: false, script: "return['Could not get The environments']" ], script: [ classpath: [], sandbox: false, script: """ if (TESTING_METHOD.equals('Tags')) { return '<form onsubmit="handleSubmit(event)">' + '<input type="hidden" name="value" id="hiddenValue">' + '<input name="value[]" type="checkbox" value="fast"> Fast<br>' + '<input name="value[]" type="checkbox" value="slow"> Slow<br>' + '<input name="value[]" type="checkbox" value="Medium"> Medium<br>' + '<input name="value[]" type="checkbox" value="Rapid"> Rapid<br>' + '<script>' + 'function handleSubmit(e) {' + ' e.preventDefault();' + ' const checkedBoxes = document.querySelectorAll(\'input[name="value[]"]:checked\');' + ' const values = Array.from(checkedBoxes).map(box => box.value).join(\',\');' + ' document.getElementById(\'hiddenValue\').value = values;' + ' return true;' + '}' + '</script>' + '</form>' } else { return '<input name="value" type="text" value="Intel Core i5" disabled>' } """ ] ] ],
说明
- 隐藏字段
hiddenValue负责将选中值以逗号分隔的字符串形式传递给Jenkins的TAGS参数 - JavaScript函数
handleSubmit在表单提交前触发,自动收集所有选中的复选框值并拼接 - 后续流水线中可通过
TAGS.split(',')将字符串拆分为数组,处理多个选中值
内容的提问来源于stack exchange,提问作者Ranveer Raj
相关产品推荐
相关产品推荐

