Jenkins: Using parameters in pipeline.
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 :-
Give the name of pipeline job.
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}
.
Build the above job: As shown below the job is running.
After first build the name ”Build with Parameters” will show up.
Try to build with parameters: