如何根据另一个参数的条件创建可编辑参数?

How to create editable parameter based on conditions of another parameter?

对于 Jenkins 管道,我有一个参数说 Repository,第二个参数说 Branch

基于 Repository 值,我想自动填充 Branch 的值。

假设 -

如果 RepositoryBobRepo 那么对于 BranchBobBranch 是自动填充的

如果 RepositoryAdamRepo 那么对于 BranchAdamBranch 是自动填充的

这可以使用 Active Choice Reactive Reference Parameter 来实现。

但是,如果用户向 Repository 提供了一些未知值,例如 UnknownRepo,那么他应该被允许在 Branch 参数中输入值,而 [=22= 是不可能的]

请问如何在条件不匹配的情况下实现可编辑参数?

实际上你可以使用 Active Choice Reactive Reference Parameter.
您只需将 Branch 参数设置为 Choice Type Formatted HTML 并使用以下模板在 official documentation:

中提供
return "<input name=\"value\" value=\"${ReactiveRefParam}\" class=\"setting-input\" type=\"text\">"

此代码模板将使用 value 属性中定义的传播值创建一个输入参数,并使用户能够编辑传播值。
在您的情况下,它看起来像:

def branch = ''
if (Repository == 'BobRepo') {
   branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
   branch = 'AdamBranch'
}
return "<input name='value' value='${branch}' class='setting-input' type='text'>"

如果您希望输入字段只读某些值,那么也可以使用 HTML 输入的 readonly 属性和一些逻辑。
此外,您还可以根据需要为输入添加HTML样式。
这是一个在填充分支并将输入宽度更改为自定义值时防止编辑的示例:

def branch = ''
if (Repository == 'BobRepo') {
   branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
   branch = 'AdamBranch'
}
return "<input name='value' value='${Repository}' style='width: 200px;' class='setting-input' type='text' ${branch ? 'readonly' : ''}>"