我们可以使用 Active Choices Plugin 生成文本字段参数。
我们将在这里使用两种类型的参数:Active Choices Parameter 和 Active Choices Reactive Reference Parameter
Active Choices 反应参考参数
在 Active Choices Reactive Reference Parameter 中,有多种渲染选项可用,其中一种是 Formatted HTML,即在返回字符串中您可以传递 html 标签。
因此,利用上述两个参数,以下是对您的情况有所帮助的管道代码:
注意:保存下面的管道代码后,先点击Build Now。构建成功后,刷新页面。您将能够看到选项Build with Parameters。您还可以在作业配置页面中看到,在保存管道后构建作业后,This build is parameterized 的所有字段都会自动填充。
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_CHECKBOX',
description: 'Select the Application Service from the Dropdown List',
filterLength: 1,
filterable: false,
name: 'data_center',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
"return['Could not get the services list']"
],
script: [
classpath: [],
sandbox: false,
script:
"return['DC01', 'DC02', 'DC03']"
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'hostname',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC01')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
} else
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'ipaddress',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC01')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
} else
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'port_number',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
]
])
])
pipeline {
environment {
vari = ""
}
agent any
stages {
stage ("Example") {
steps {
script{
echo "${params.data_center}"
echo '\n'
echo "${params.hostname}"
echo "${params.ipaddress}"
echo "${params.port_number}"
}
}
}
}
}
截图:
选择数据中心DC01时的输出,
选择数据中心DC02时的输出,