jenkins插件Active Choices Plug-in使用心得

软件环境

  • Active Choices Plug-in 2.7.2
  • Jenkins 2.414.1 (docker: jenkins/jenkins:lts-jdk11)

Groovy中调用shell

// Choice下拉列出当前工作目录的所在子目录
def proc = "ls".execute()
proc.waitFor()
def output = proc.in.text
return output.split("\n") as ArrayList

Input text box 不可编辑

Input text box是Active Choices Reactive Reference Parameter里的一个选项,但它的使用非常奇怪。官方wiki,提到它只需要返回一个字符串,但是。。。但是。。。我困惑于这个UI控件的文本居然不可编辑,刚开始我一度认为是不是漏了设置什么选项导致的。但最后通过浏览器F12查看对应的html结构,该控件确实是不可编辑。。。(那为什么叫Input text box???)

可编辑输入框

你可以通使用Jenkins内置的控件String Parameter来创建输入框,但本文的主题是第三方插件Active Choices Plug-in,它能通过groovy script可以更灵活更复杂地控制输入框控件。这里用到Active Choices Reactive Reference Parameter的Formatted HTML选项,需要groovy返回符合html格式的字符串,而且更重要的是。。。更重要的是 。。。必须有一个HTML标签的名称是value,即 name=”value”,因为这是插件约定了只有这个标签的值会被传递到构建过程参考

以下分别两个样例,最终均是在shell输出输入框的内容。其中的区别只是一个HTML标签name=’foo’,另一个符合插件要求的name=’value’。执行结果是前者$a输出是空的,后者$a输出’输入你的内容’

  1. name=’foo’
"<input name='foo' class='setting-input' type='text' value='输入你的内容'>"

  1. name=’value’,其它参数同上
"<input name='value' class='setting-input' type='text' value='输入你的内容'>"

如果你仔细看,上图$a尾部多了输出一个逗号符号,这不是我手抖多打了个符号。插件wiki对此有解释,大意是插件了担心你没有提供name=’value’的标签导致不符合预期的结果,于是创建了一个不可见的name=’value’的input标签。但当你主动填写name=’value’的标签时,这个内部创建的不可见并不会取消,最终就有两个name=’value’的标签。如果不需要这个隐藏的标签,需要勾上Omit value field选项 Advanced Option: Omit Value Field

By default, ‘Reactive References’ pass to the build a hidden . It means that your ‘Reactive Reference’ parameter will always be empty, but you can use a ‘Formatted HTML’ parameter and instruct the plug-in to not include this hidden value parameter. You can click the ‘Advanced’ button and there you will find an option to omit the value field. This will you let you define a value for the hidden parameter.

多个name=’value’标签的值,返回给$a会是数组吗?毕竟逗号分隔确实也让我也有这方面的猜想,实验结果是$a是以逗号拼接多个值的字符串。 我另外开了一个pipleline工程,多增加一个input标签,并且在pipeline里打印$a的值和$a的getClass

原文:
https://lizijie.github.io/2023/09/09/jenkins%E6%8F%92%E4%BB%B6Active-Choices-Plug-in%E4%BD%BF%E7%94%A8%E5%BF%83%E5%BE%97.html
作者github:
https://github.com/lizijie

PREVIOUSdocker笔记
NEXTSourceTree自定义菜单命令 拉取所有仓库最新代码