Jenkins Job 构建动态参数配置

在做jenkins项目时,有些参数不是固定写死的,而是动态变化的,这时我们可以用 Active Choices 插件来远程调用参数。

实现结果如下:

操作步骤:

 注意:筛选关键字参数必须位于筛选列表的前面,如该文章示例keyword必须在affectedPlatformIds前面

 

 

 Groovy Script代码如下:

import groovy.json.JsonSlurper
def respText = "curl http://192.168.1.29:81/upgrade_platform/allList?keyword=${keyword}".execute().text
def slurper = new groovy.json.JsonSlurper() 
def pfm = slurper.parseText(respText) //parseText将json字符串转换为Map对象;JsonOutput类用于将Groovy对象转换为JSON字符串

html_to_be_rendered = "<div style=\"max-height:300px;overflow-y: scroll;width: 350px;\"><table><tr>"
pfm.each {
    html_to_be_rendered = """
    ${html_to_be_rendered}
    <tr>
    <td>
    <input name=\"value\" alt=\"${it.id}\" json=\"${it.id}\" type=\"checkbox\" class=\" \">
    <label title=\"${it.platform_name}\" class=\" \">${it.platform_name}</label>
    </td>
    </tr>
"""
}


html_to_be_rendered = "${html_to_be_rendered}</tr></table></div>"

return html_to_be_rendered

管理员用户登录后,允许上面的groovy脚本执行:

 

 

 

 其他代码groovy代码备份:

Active Choices Reactive Parameter

import groovy.json.JsonSlurper
def respText = "curl http://192.168.1.29:81/upgrade_platform/getAllPlatformName?keyword=${keyword}".execute().text
def slurper = new groovy.json.JsonSlurper() 
def pfm = slurper.parseText(respText) //parseText将json字符串转换为Map对象;JsonOutput类用于将Groovy对象转换为JSON字符串
return pfm

 

效果:

代码: 

Active Choices Parameter
Active Choices Reactive Reference Parameter

return ["web", "backend", "database"]
service_tier_map = [
  "web": [
    ["service_name": "user_frontend", "release_tag": "1.0.0" ],
    ["service_name": "admin_frontend", "release_tag": "1.0.2" ],
  ],
  "backend": [
    ["service_name": "admin_service", "release_tag": "2.1.0" ],
    ["service_name": "finance_service", "release_tag": "2.2.0" ],
    ["service_name": "payment_service", "release_tag": "3.2.0" ],
  ],
  "database": [
    ["service_name": "dynamo_db", "release_tag": "5.4.1"],
    ["service_name": "mysql", "release_tag": "3.2.1"],
    ["service_name": "postgresql", "release_tag": "1.2.3"],
  ],
]

html_to_be_rendered = "<table><tr>"
service_list = service_tier_map[tier]
service_list.each { service ->
  html_to_be_rendered = """
    ${html_to_be_rendered}
    <tr>
    <td>
    <input name=\"value\" alt=\"${service.service_name}\" json=\"${service.service_name}\" type=\"checkbox\" class=\" \">
    <label title=\"${service.service_name}\" class=\" \">${service.service_name}</label>
    </td>
    <td>
    <input type=\"text\" class=\" \" name=\"value\" value=\"${service.release_tag}\"> </br>
    </td>
    </tr>
"""
}


html_to_be_rendered = "${html_to_be_rendered}</tr></table>"

return html_to_be_rendered

 

 
posted @ 2024-09-04 19:36  雪洁  阅读(91)  评论(0编辑  收藏  举报