Jenkins动态控制参数集展示:按前置参数调整次数及标题
解决思路
核心依赖Jenkins的Active Choices Plugin实现动态参数渲染,步骤如下:
1. 安装必要插件
确保Jenkins已安装Active Choices Plugin(原生Jenkins不支持动态参数的自定义渲染)。
2. 创建前置数字参数
添加一个Choice Parameter(或Number Parameter),名称设为PARAMETER_COUNT,选项配置为0、1、2、3,默认值设为0。这个参数用来控制后续展示的参数组数量。
3. 添加动态参数组
添加一个Active Choices Reactive Reference Parameter,配置如下:
- 名称:任意命名(比如
DYNAMIC_PARAM_SETS) - 参考参数:勾选
PARAMETER_COUNT(让参数随前置选择动态变化) - 脚本类型:选择
Groovy Script,填入以下逻辑代码:
def count = PARAMETER_COUNT.toInteger() def htmlOutput = "" if (count > 0) { // 定义序数词映射 def ordinalLabels = ["1st", "2nd", "3rd"] for (int i = 0; i < count; i++) { def currentOrdinal = ordinalLabels[i] // 生成对应序号的参数组HTML,可根据需求修改参数类型/数量 htmlOutput += """ <div style="margin: 16px 0; padding-bottom: 12px; border-bottom: 1px solid #eee;"> <h3 style="color: #428bca; margin: 0 0 8px;">${currentOrdinal} PARAMETERS</h3> <div style="margin: 6px 0;"> <label style="display: inline-block; width: 120px;">参数X:</label> <input type="text" name="PARAM_X_${i+1}" placeholder="输入${currentOrdinal}组参数X" required> </div> <div style="margin: 6px 0;"> <label style="display: inline-block; width: 120px;">参数Y:</label> <select name="PARAM_Y_${i+1}"> <option value="opt1">选项1</option> <option value="opt2">选项2</option> </select> </div> </div> """ } } return htmlOutput
- 额外配置:勾选Allow HTML(让Jenkins渲染脚本输出的HTML内容),将Visible Item Count设为
15(避免参数内容被截断)。
4. 参数引用方式
任务运行时,提交的动态参数可通过变量名引用,比如第一组的参数X为PARAM_X_1,第二组的参数Y为PARAM_Y_2,以此类推。
关键说明
- 若需要其他类型的参数(如密码框、文件选择),可在Groovy脚本中对应修改HTML表单元素(比如
<input type="password">),Jenkins会自动识别并处理这些参数的提交。 - 当
PARAMETER_COUNT选择0时,脚本输出空内容,不会展示任何参数组,符合需求。
内容的提问来源于stack exchange,提问作者Sawo
相关产品推荐
相关产品推荐

