gpt4 book ai didi

Jenkins 根据另一个参数值生成新参数

转载 作者:行者123 更新时间:2023-12-04 14:11:00 24 4
gpt4 key购买 nike

我有一个名为“数据中心”的选择参数,它有两个值 DC01 和 DC02。如果用户选择 DC01,我希望显示其他一些构建参数,如果他选择 DC02,我希望显示其他参数。有什么办法吗?

如果用户选择 DC01,我需要另外两个字段,例如“主机名”和“IP 地址”,它们只是文本字段。

我尝试过使用 Active Choice Plugin,但我认为我们无法使用它生成文本字段参数。

有人能帮忙吗?

最佳答案

我们可以使用 Active Choices 插件生成文本字段参数。

我们将在这里使用两种类型的参数:Active Choices ParameterActive Choices Reactive Reference Parameter

Active Choices Reactive Reference Parameter

在 Active Choices Reactive Reference Parameter 中,有多种可用的呈现选项,其中一个是 Formatted HTML 即在返回字符串中您可以传递 html 标签。

因此,利用上述两个参数,以下是对您的情况有帮助的管道代码:

Note: After saving the below pipeline code, first Click on Build Now. After the build got success, then refresh the page. You will be able to see the option Build with Parameters. You can also see in the Job Configuration page, all the fields of This build is parameterized gets populated automatically once you build the job after saving the pipeline.

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}"

}
}
}
}
}

截图:

enter image description here

选择数据中心DC01时的输出,

enter image description here

enter image description here

选择数据中心DC02时的输出,

enter image description here

enter image description here

关于Jenkins 根据另一个参数值生成新参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64395488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com