Is it possible to have a Jenkins parameter's name with spaces in it?

Viewed 3110

I'm trying to make my Jenkins UI more clean.
My Jenkins file calls a function which in turn runs the following:

properties ([
    [$class: 'GitLabConnectionProperty', gitLabConnection: 'GitlabConnection'],
    [$class: 'ParametersDefinitionProperty', parameterDefinitions: [
      [$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'activateInTest'],
      [$class: 'ChoiceParameterDefinition', choices: 'false\ntrue\n', description: 'If running newBuild, skip unit tests', name: 'skipUnitTests']
    ]]
  ])

Currently, I can access these parameters like this:

if(activateInTest == 'true') {
    //Do something
}

After going through other docs and examples. It looked as if I could also access parameters by doing something like params.activateInTest, which did not work. I also tried doing something like params["activateInTest"], but that didn't work either.

The reason I want to access it this way params["..."], is because I would like to have the name of my parameter be "Activate in Test" rather than "activateInTest".

In this example I see the person does use "BooleanParameterDefinition" with spaces in the name. But I can't seem to figure out how to use spaces in the name. Having spaces in the name is my only goal here.

3 Answers

yes, its possible, just use following notation:

${params['Name with space']}

tested on old Jenkins: 2.149

Indeed it is possible, user "string reference" to access it, i.e. params."Activate in Test"

For example:

properties([parameters([
    string(name: 'Activate in Test', defaultValue: 'default value')
])])

echo params."Activate in Test"

In Java and Groovy space in a variable does not support! and it's not recommended but Jenkins supports it with 'String referencing' But If you want to decorate the parameter Display Name it would be something like this

Jenkins Declarative Pipeline

pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')

        text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"

                echo "Biography: ${params.BIOGRAPHY}"

                echo "Toggle: ${params.TOGGLE}"

                echo "Choice: ${params.CHOICE}"

                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

enter image description here

Scripted Pipeline

node {
  properties(
    [
        parameters(
            [string(defaultValue: '/data', name: 'Directory', description: "Directort Path"),
             string(defaultValue: 'Dev', name: 'DEPLOY_ENV', description: "Deploy Environment")
            ]
        )

    ]
  )    

  stage('debug') {
      echo "${params}"
  }
}

enter image description here

Related