Jenkins声明性管道:参数指令中的if-else语句



如果我有选项可供选择,我将尝试显示一个选择参数,或者显示一个输入文本,类似这样的东西(不起作用(:

pipeline {
    agent any
    parameters {
       if (someOptions) {
          choice(name: 'FIELD_NAME', choices: "$someOptions", description: 'Field description')
       } else {
          string(name: 'FIELD_NAME', defaultValue: '', description: 'Field description')
       }
    }
    environment {
      // environment params
    }
    stages {
      // stages
    }
}

有办法做到这一点吗?

要扩展@Matt Schuchard的评论,下面是它可能看起来的样子:

def my_param = []
if (someOptions) {
     my_param = [$class: 'ChoiceParameter',
            name: 'FIELD_NAME',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Choose the desired option',
            script:
                [$class: 'GroovyScript',
                  fallbackScript:
                    [classpath: [], sandbox: false, script: 'return ""'],
                  script:
                    [classpath: [], sandbox: false, script: "return $someOptions"]
                ]
            ]
} else {
    my_param = [$class: 'StringParameterDefinition',
            name: 'FIELD_NAME',
            defaultValue: false,
            description: '']
}
properties([
    parameters([my_param,
          // other parameters

不要忘记在脚本审批控制台中审批Groovy脚本。

最新更新