Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: How to use choice parameter in declarative pipeline?

My example:

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

outputs with error:

"  unexpected char: '\'  " on the line with "choices" "

Following these instructions: https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Parametrized-pipelines

Any idea or advice on what I do wrong?

like image 929
user1316502 Avatar asked Dec 27 '17 14:12

user1316502


People also ask

How do you mention choice parameters in Jenkins pipeline?

You just have to use params. [NAME] in places where you need to substitute the parameter. Here is an example of a stage that will be executed based on the condition that we get from the choice parameter. The parameter name is ENVIRONMENT , and we access it in the stage as params.

How do you pass parameters in Jenkins declarative pipeline?

Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do I add extended choice parameters in Jenkins?

To install this plugin, you simply need to go to your jenkins instance and navigate to “/pluginManager/available”, and then search for “Extended Choice Parameter Plug-in” in the search box. Be careful! There is another plugin called “Extensible Choice Parameter Plug-in”.


2 Answers

The documentation for declarative jenkins pipelines says:

A choice parameter, for example:

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

First one is the default value

like image 147
Boris Avatar answered Oct 23 '22 04:10

Boris


You need to use \n instead of \. See this code:

  pipeline {
  agent any
  parameters {
    choice(
        name: 'myParameter',
        choices: "Option1\nOption2",
        description: 'interesting stuff' )
  }
}
like image 36
Alexandre Muzulão Avatar answered Oct 23 '22 05:10

Alexandre Muzulão