持续集成 Jenkins 插件——Active Choices Plugin (一)

咚咚 · 2019年04月05日 · 最后由 fengyu 回复于 2020年12月31日 · 3840 次阅读

Active Choices Plugin 插件

官方定义:

The Active Choices plugin allows the creation of dynamic and interactive parameters for freestyle Jenkins jobs. Active Choice parameters can be dynamically updated and can be rendered as combo-boxes, check-boxes, radio-buttons or rich HTML.

安装方法

直接进入插件管理中心搜索 Active Choices 安装即可。

使用

Active Choices Parameter

1、参数化构建中选择 *Active Choices Parameter *
2、Groovy script 编写具体脚本,如下:

图中是读取文件的内容,也可以直接 return [1,2,3] 使用。

3、Choice Type:可以选择类型,可以设置为单选和多选,还有 radio buttons 这种单选框,check box 是复选框,可以根据自己的需求来使用

4、Fallback Script:当上面 Groovy script 执行出错的时候会展示 Fallback Script 中定义的执行结果

Active Choices Reactive Parameter

Active Choices 插件还有一个好用的功能,就是当有多个参数时,第二个参数的值,会根据第一个参数选择的值进行变换。

例如:我们出很多渠道包,渠道码需要根据不同的账号或者规则去展示。那就需要参数是变化的
1、参数化构建中选择 Active Choices Reactive Parameter
2、Groovy script 编写具体脚本,如下:

图中是直接定义的结果,也可以读取文件
3、Referenced parameters:需要参考的参数,如果是根据第一个参数变化的,则填写参数 1 的名称
4、Enable filters:勾选,则支持过滤

Active Choices Reactive Reference Parameter

作展示,根据脚本展示不同的结果。如下图所示;

说明:默认【Active Choices Reactive Reference Parameter 】的值,不传递到构建环境的,如果需要传递到构建环境,需要如下使用:

最终界面展示如下:

#### 参考文档
1、收集了各种脚本:https://github.com/jenkinsci/jenkins-scripts/tree/master/scriptler
2、官方文档:https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin

共收到 5 条回复 时间 点赞

想问下。楼主的文件的 txt 文件是放在哪里的,放在 jenkins 安装机器的那个路径下?

请教楼主,如何调用系统上的脚本以及传参😭 ?

卡布 回复

jenkins 所在机器的任何可访问路径都可以的

Panwenbin1 回复

你是说在 groovy script 调用系统的脚本么?我没有这样处理过呢。

active choice 文档中有提到两个变量jenkinsProjectjenkinsBuild,大家一起看下怎么在 script 脚本中使用?我现在的需求是希望可以取到 jenkins 管理的 credentials 帐号密码,当前还没有找到方法。

  • jenkinsProject -> The Jenkins Project object
  • jenkinsBuild -> The Jenkins Build object


附上我找到的一些使用方法

 properties([
                parameters([
                        [$class: 'ChoiceParameter',
                         choiceType: 'PT_SINGLE_SELECT',
                         description: 'Select the Env Name from the Dropdown List',
                         filterLength: 1,
                         filterable: false,
                         name: 'Env',
                         randomName: 'choice-parameter-5631314439613978',
                         script: [
                                 $class: 'GroovyScript',
                                 fallbackScript: [
                                         classpath: [],
                                         sandbox: false,
                                         script:
                                                 'return[\'Could not get Env\']'
                                 ],
                                 script: [
                                         classpath: [],
                                         sandbox: false,
                                         script:
                                                 'return["Dev","QA","Stage","Prod"]'
                                 ]
                         ]
                        ],
                        [$class              : 'DynamicReferenceParameter',
                         choiceType          : 'ET_FORMATTED_HTML',
                         omitValueField      : true,
                         description         : 'Please provide a Elastic alias label',
                         name                : 'PC_CPU',
                         randomName          : 'choice-parameter-5631314456178624',
                         referencedParameters: 'NeedUpgradePC',
                         script              : [
                                 $class        : 'GroovyScript',
                                 fallbackScript: [
                                         classpath: [],
                                         sandbox  : true,
                                         script   :
                                                 'return[\'nothing.....\']'
                                 ],
                                 script        : [
                                         classpath: [],
                                         sandbox  : true,
                                         script   :
                                                 """
                                        inputBox="<input name='value' type='text' value='Intel Core i5'>"
                                """
                                 ]
                         ]
                        ],
                        [$class: 'CascadeChoiceParameter',
                         choiceType: 'PT_CHECKBOX',
                         description: 'Select Servers',
                         filterLength: 1,
                         filterable: true,
                         name: 'Server',
                         randomName: 'choice-parameter-5631314456178619',
                         referencedParameters: 'Env',
                         script: [
                                 $class: 'GroovyScript',
                                 fallbackScript: [
                                         classpath: [],
                                         sandbox: false,
                                         script:
                                                 'return[\'Could not get Environment from Env Param\']'
                                 ],
                                 script: [
                                         classpath: [],
                                         sandbox: false,
                                         script:
                                                 ''' if (Env.equals("Dev")){
                            return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                        }
                        else if(Env.equals("QA")){
                            return["qaaaa001","qabbb002","qaccc003"]
                        }
                        else if(Env.equals("Stage")){
                            return["staaa001","stbbb002","stccc003"]
                        }
                        else if(Env.equals("Prod")){
                            return["praaa001","prbbb002","prccc003"]
                        }
                    '''
                                 ]
                         ]
                        ]
                ])

//parameters([
//        choice(name: "NeedUpgradePC", choices: ['yes', 'no'], description: "Do you need upgrade your PC"),
//        [$class              : 'DynamicReferenceParameter',
//         choiceType          : 'ET_FORMATTED_HIDDEN_HTML',
//         omitValueField      : true,
//         description         : 'Please provide a Elastic alias label',
//         name                : 'PC_RAM',
//         randomName          : 'choice-parameter-5631314456178624',
//         referencedParameters: 'NeedUpgradePC',
//         script              : [
//                 $class        : 'GroovyScript',
//                 fallbackScript: [
//                         classpath: [],
//                         sandbox  : true,
//                         script   :
//                                 'return[\'nothing.....\']'
//                 ],
//                 script        : [
//                         classpath: [],
//                         sandbox  : true,
//                         script   :
//                                 """
//                                    if(NeedUpgradePC.equals('yes')) {
//                                        inputBox="<input name='value' type='text' value='Kingston 8GB'>"
//                                    } else {
//                                        inputBox="<input name='value' type='text' value='Kingston 8GB' disabled>"
//                                    }
//                                """
//                 ]
//         ]
//        ],
//        [$class              : 'DynamicReferenceParameter',
//         choiceType          : 'ET_FORMATTED_HTML',
//         omitValueField      : true,
//         description         : 'Please provide a Elastic alias label',
//         name                : 'PC_CPU',
//         randomName          : 'choice-parameter-5631314456178624',
//         referencedParameters: 'NeedUpgradePC',
//         script              : [
//                 $class        : 'GroovyScript',
//                 fallbackScript: [
//                         classpath: [],
//                         sandbox  : true,
//                         script   :
//                                 'return[\'nothing.....\']'
//                 ],
//                 script        : [
//                         classpath: [],
//                         sandbox  : true,
//                         script   :
//                                 """
//                                    if(NeedUpgradePC.equals('yes')) {
//                                        inputBox="<input name='value' type='text' value='Intel Core i5'>"
//                                    } else {
//                                        inputBox="<input name='value' type='text' value='Intel Core i5' disabled>"
//                                    }
//                                """
//                 ]
//         ]
//        ]
//
//])
        ])
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册