如何在jenkins管道中设置默认选项?

red*_*888 18 jenkins jenkins-plugins jenkins-pipeline

很沮丧我找不到这个例子.如何设置默认选项?

parameters {
    choice(
        defaultValue: 'bbb',
        name: 'param1',
        choices: 'aaa\nbbb\nccc',
        description: 'lkdsjflksjlsjdf'
    )
}
Run Code Online (Sandbox Code Playgroud)

defaultValue在这里无效.我希望选择是可选的,并且如果管道是非手动运行(通过提交),则设置默认值.

mko*_*bit 29

您无法在选项中指定默认值.根据choice输入的文档,第一个选项将是默认选项.

潜在的选择,每行一个.第一行的值将是默认值.

您可以在文档源中看到这一点,以及如何在源代码中调用它.

return new StringParameterValue(
  getName(), 
  defaultValue == null ? choices.get(0) : defaultValue, getDescription()
);
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,如果它是非交互触发的,那么param.param1将是首选还是它为null? (2认同)

Chr*_*son 7

正如 mkobit 所说,defaultValue参数似乎不可能,而是我根据上一个选择重新排序了选择列表

defaultChoices = ["foo", "bar", "baz"]
choices = createChoicesWithPreviousChoice(defaultChoices, "${params.CHOICE}")

properties([
    parameters([
        choice(name: "CHOICE", choices: choices.join("\n"))
    ])   
])


node {
    stage('stuff') {
        sh("echo ${params.CHOICE}")
    }
}

List createChoicesWithPreviousChoice(List defaultChoices, String previousChoice) {
    if (previousChoice == null) {
       return defaultChoices
    }
    choices = defaultChoices.minus(previousChoice)
    choices.add(0, previousChoice)
    return choices
}
Run Code Online (Sandbox Code Playgroud)