In a scripted Jenkins pipeline, you can use different types of variables depending on your requirements. Here are examples of different variable types commonly used in scripted pipelines:
In a scripted pipeline, you can define variables using the def
keyword. Here’s an example of how to define a variable in a scripted Jenkins pipeline:
node {
stage('Example') {
// Define variable using def
def myVariable = "Hello, Jenkins!"
// Access and use the variable
echo myVariable
// You can also use the variable in conditional statements or within steps
if (myVariable == "Hello, Jenkins!") {
echo "Variable value matches!"
} else {
echo "Variable value does not match!"
}
}
}
In the above example, we define a variable myVariable
using the def
keyword and assign it the value "Hello, Jenkins!"
. We then echo the variable’s value and demonstrate its usage within an if
statement.
Note that the node
block is used to allocate a Jenkins agent to execute the pipeline steps. Inside the stage
block, you can define and use variables as needed.
Types of variables in Scripted Pipeline
String Variable:
node {
stage('Example') {
// Define a string variable
def myString = "Hello, Jenkins!"
// Access and use the string variable
echo myString
}
}
Integer Variable:
node {
stage('Example') {
// Define an integer variable
def myInteger = 10
// Access and use the integer variable
echo myInteger
}
}
Boolean Variable:
node {
stage('Example') {
// Define a boolean variable
def myBoolean = true
// Access and use the boolean variable
if (myBoolean) {
echo "The boolean variable is true."
} else {
echo "The boolean variable is false."
}
}
}
List Variable
node {
stage('Example') {
// Define a list variable
def myList = ["apple", "banana", "orange"]
// Access and use the list variable
for (item in myList) {
echo item
}
}
}
Map Variable:
node {
stage('Example') {
// Define a map variable
def myMap = [name: "John", age: 30, city: "New York"]
// Access and use the map variable
echo "Name: ${myMap.name}"
echo "Age: ${myMap.age}"
echo "City: ${myMap.city}"
}
}
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND
thanks for sharing all types of variables Scripting pipeline that developer use regularly that helps lot