Sitemap

Jenkins: Using parameters in pipeline.

2 min readDec 8, 2024

Parameterized Pipelines in Jenkins:

Build parameters in Jenkins are variables that allow users to pass dynamic values to Jenkins jobs at runtime, enhancing the flexibility and reusability of pipeline scripts.

Types of Parameters:

Jenkins supports several types of parameters that can be utilized in both declarative and scripted pipelines:

  • String Parameter: Accepts a single line of text input.
  • Boolean Parameter: Represents a true/false value.
  • Choice Parameter: Allows users to select from a predefined list of options.
  • Text Parameter: Accepts multi-line text input.
  • File Parameter: Lets users upload files as part of the build process.

Example of Defining Parameters

To define parameters in a Jenkins pipeline, you can use the following syntax:

pipeline {
agent any
parameters {
string(name: 'SAMPLE_STRING', defaultValue: 'default', description: 'A sample string parameter')
booleanParam(name: 'SAMPLE_BOOLEAN', defaultValue: true, description: 'A boolean parameter')
choice(name: 'GIVE_CHOICE', choices: ['Ansible', 'Kubernetes'], description: 'Choose one option')
}
stages {
stage('Example') {
steps {
script {
echo "String parameter value: ${params.SAMPLE_STRING}"
echo "Boolean parameter value: ${params.SAMPLE_BOOLEAN}"
echo "Choice parameter value: ${params.FIVE_CHOICE}"
}
}
}
}
}

Steps to create Parameterized pipeline job :-

Press enter or click to view image in full size
Create-new parametarize pipeline job

Give the name of pipeline job.

Press enter or click to view image in full size
input the pipeline job name

In this example, three different types of parameters are defined. The parameters can be accessed within the pipeline using the params object, such as ${params.SAMPLE_STRING}.

Press enter or click to view image in full size
create the declarative pipeline.

Build the above job: As shown below the job is running.

Press enter or click to view image in full size

After first build the name ”Build with Parameters” will show up.

Press enter or click to view image in full size

Try to build with parameters:

Press enter or click to view image in full size

--

--

No responses yet