Jenkins - 动态关联参数 Active Choices Reactive Parameters
在创建 Jenkins Job,需要设置一些参数,有时想偷懒,在手动触发 Build 时不想输入太多参数,如果一些参数能互相关联就好了。这款 Active Choices Reactive Parameters 类型参数可以实现。
引言
在创建 Jenkins Job,需要设置一些参数,有时想偷懒,在手动触发 Build 时不想输入太多参数,如果一些参数能互相关联就好了。这款 Active Choices Reactive Parameters
类型参数可以实现。
内容提要:
- 安装 Active Choices Plugin
- UI 应用 Active Choices Reactive Parameters
- Pipeline 脚本应用
安装 Active Choices Plugin
在用 Active Choices Reactive Parameters
之前,确保 Jenkins 上安装 Active Choices
插件。
Manage Jenkins -> pluginManager
安装成功后,打开 Job Configure 页面,勾上 This project is parameterized
, 点开 Add Parameter
,会看到多出下面三个选项。
应用 Active Choices Reactive Parameters
达成的效果如下:根据 Env 和 Project_Name 的数据动态生成 Smoke_Job_Name
首先添加 Env 和 Project_Name 参数:选择 Extensible Choice
参数类型
注意不要勾选 Editable,本人试过,动态参数不会动态生成
。
添加动态关联参数:选择 Active Choices Reactive Parameters 参数类型,具体介绍请参考官网 Active Choice
Fallback Script, 是当脚本运行失败或抛异常的处理。
Groovy 脚本:
if(Project_Name.equals("A") && Env.equals("Dev")){
return ["Dev/API-Test/A_Dev_Smoke"]
}else if(Project_Name.equals("A") && Env.equals("Prod")){
return ["Dev/API-Test/A_Prod_Smoke"]
}else if(Project_Name.equals("B") && Env.equals("Dev")){
return ["Dev/API-Test/B_Dev_Smoke"]
}else if(Project_Name.equals("B") && Env.equals("Prod")){
return ["Dev/API-Test/B_Prod_Smoke"]
}else{
return ["Not Ready"]
}
如果 Choice Type 选择 Multi Select,Groovy 脚本中也可以返回多个 Item, 例如:
return [“item 1”, “item 2”]
Pipeline 脚本应用
先看一下达成的效果:根据 Env 参数,动态绑定 Server 参数
Pipeline Groovy 脚本:
注意: sandbox: true
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: true,
script:
'''return["Could not get Env"]'''
],
script: [
classpath: [],
sandbox: true,
script:
'''return["Dev","QA","Stage","Prod"]'''
]
]
],
[$class: "CascadeChoiceParameter",
choiceType: "PT_SINGLE_SELECT",
description: "Select the Server from the Dropdown List",
filterLength: 1,
filterable: false,
name: "Server",
randomName: "choice-parameter-5631314456178619",
referencedParameters: "Env",
script: [
$class: "GroovyScript",
fallbackScript: [
classpath: [],
sandbox: true,
script:
'''return["Could not get Environment from Env Param"]'''
],
script: [
classpath: [],
sandbox: true,
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"]
}
'''
]
]
]
])
])
pipeline {
environment {
vari = ""
}
agent any
stages {
stage ("Example") {
steps {
script{
echo 'Hello'
echo "${params.Env}"
echo "${params.Server}"
if (params.Server.equals("Could not get Environment from Env Param")) {
echo "Must be the first build after Pipeline deployment. Aborting the build"
currentBuild.result = 'ABORTED'
return
}
echo "Crossed param validation"
} }
}
}
}
更多推荐
所有评论(0)