Skip to content

How to use Choice build parameters in Jenkinsfile

In this tutorial, we will learn how to add choice parameters in Jenkinsfile, which allows users to select from a predefined list of options when triggering the pipeline. This is useful for restricting input values to a valid set, reducing errors.

We can achieve this using the parameters block and the choice parameter type in Jenkinsfile.

Jenkinsfile

Here is the sample Jenkinsfile 14-Jenkinsfile-maven-build-parameters-choice

pipeline {
  agent any
  options {
    disableConcurrentBuilds()
    disableResume()
    buildDiscarder(logRotator(numToKeepStr: '10'))
    timeout(time: 1, unit: 'HOURS')
  }
  tools {
    maven 'maven-3.6.3' 
  }
  parameters {
    choice(name: 'MAVEN_GOAL', choices: ['compile', 'test', 'package', 'install'], description: 'Maven goal')
  }
  stages {
    stage ('Build') {
      steps {
        sh "mvn clean ${params.MAVEN_GOAL}"
      }
    }
  }
  post {
    always {
      deleteDir()
    }
  }
}

Explanation

In the standard Jenkins pipeline syntax, the parameters block is used to define input parameters.

Inside the parameters block, we are defining a parameter named MAVEN_GOAL using the choice directive.

  • name: The name of the parameter variable (e.g., 'MAVEN_GOAL'). This variable can be accessed in the pipeline steps.
  • choices: A list of valid options presented as a dropdown menu (e.g., ['compile', 'test', 'package', 'install']). The first item in the list is the default selection.
  • description: A helpful description shown to the user in the Jenkins UI.

In the Build stage, we access the value of the selected choice using ${params.MAVEN_GOAL}.

sh "mvn clean ${params.MAVEN_GOAL}"

When you run this pipeline for the first time, Jenkins will register the parameters. On subsequent runs, you will see a "Build with Parameters" option in the Jenkins UI.

Clicking "Build with Parameters" will show a dropdown menu where you can select one of the provided options: compile, test, package, or install.

If you select package, the command executed will be mvn clean package.

Reference

๐Ÿง  Quick Quiz โ€” Choice Parameters

#

Which parameter type provides a dropdown list of options for the user to select from in the Jenkins UI?

๐Ÿ“ฌ DevopsPilot Weekly โ€” Learn DevOps, Cloud & Gen AI the simple way.
๐Ÿ‘‰ Subscribe here