如何在声明式管道中使用选择参数?

use*_*502 5 jenkins-pipeline

我的例子:

pipeline {
  agent any
  parameters {
    choice(
        name: 'myParameter',
        choices: "Option1\Option2",
        description: 'interesting stuff' )
  }
}
Run Code Online (Sandbox Code Playgroud)

输出错误:

"  unexpected char: '\'  " on the line with "choices" "
Run Code Online (Sandbox Code Playgroud)

请遵循以下说明:https : //github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Parametrized-pipelines

对我做错的任何想法或建议吗?

小智 20

声明性詹金斯管道的文档说:

一个选择参数,例如:

pipeline { 
    .....
     parameters { 
       choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') }
Run Code Online (Sandbox Code Playgroud)

第一个是默认值


小智 9

您需要使用\n而不是\。参见以下代码:

  pipeline {
  agent any
  parameters {
    choice(
        name: 'myParameter',
        choices: "Option1\nOption2",
        description: 'interesting stuff' )
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 是否有计划支持动态参数,以便我可以从调用共享库的结果中填充选择?目前缺少此关键功能。 (2认同)

小智 8

可以说是最优雅的分离选项的方法是使用像这样的数组:

pipeline {
  agent any
  parameters {
    choice(
      name: 'Env',
      choices: ['DEV', 'QA', 'UAT', 'PROD'],
      description: 'Passing the Environment'
    )
  }
  stages {
    stage('Environment') {
      steps {
        echo " The environment is ${params.Env}"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)