0
0
Jenkinsdevops~5 mins

String, boolean, and choice parameters in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Jenkins parameters let you ask users for input before running a job. String, boolean, and choice parameters help customize the job run without changing the code.
When you want to let users enter a text value like a version number before the job starts
When you want to give users a simple yes/no option to enable or disable a feature
When you want users to pick one option from a list, like selecting an environment to deploy to
When you want to reuse the same job for different inputs without making multiple jobs
When you want to make your Jenkins jobs interactive and flexible
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  parameters {
    string(name: 'VERSION', defaultValue: '1.0', description: 'Enter the version number')
    booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests before build?')
    choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'production'], description: 'Select deployment environment')
  }
  stages {
    stage('Show Parameters') {
      steps {
        echo "Version: ${params.VERSION}"
        echo "Run Tests: ${params.RUN_TESTS}"
        echo "Environment: ${params.ENVIRONMENT}"
      }
    }
  }
}

This Jenkinsfile defines a pipeline with three parameters:

  • string: lets the user enter a text value, here a version number.
  • booleanParam: lets the user choose true or false, here to run tests or not.
  • choice: lets the user pick one option from a list, here the deployment environment.

The pipeline then prints the chosen values in the first stage.

Commands
This command updates the Jenkins job with the Jenkinsfile that includes the parameters. It makes the parameters available when you run the job.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command runs the Jenkins job named 'my-job' using the Jenkins CLI. It passes values for the string, boolean, and choice parameters to customize the job run.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job -p VERSION=2.1 -p RUN_TESTS=false -p ENVIRONMENT=staging
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Version: 2.1 [Pipeline] echo Run Tests: false [Pipeline] echo Environment: staging [Pipeline] End of Pipeline Finished: SUCCESS
-p - Pass parameter values to the job
This command runs the Jenkins job without passing parameters, so default values from the Jenkinsfile are used.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-job
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Version: 1.0 [Pipeline] echo Run Tests: true [Pipeline] echo Environment: dev [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Jenkins parameters let users customize job runs by entering text, choosing yes/no, or picking from a list before the job starts.

Common Mistakes
Not defining parameters in the Jenkinsfile but trying to pass them when running the job
Jenkins won't recognize parameters that are not declared, so the job will ignore passed values or fail.
Always declare all parameters in the Jenkinsfile under the parameters block before using them.
Passing parameter values with wrong names or typos
Jenkins will ignore unknown parameter names and use defaults, causing unexpected behavior.
Use exact parameter names as declared in the Jenkinsfile when passing values.
Using choice parameter values not listed in the choices array
Jenkins will reject invalid choices and the job may fail or use defaults.
Only pass values that are included in the choices list defined in the Jenkinsfile.
Summary
Define string, boolean, and choice parameters in the Jenkinsfile to ask users for input before running a job.
Use the Jenkins CLI to run jobs and pass parameter values to customize the job execution.
If no parameters are passed, Jenkins uses the default values defined in the Jenkinsfile.