Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

Jenkins Tutorials: Variables in Scripted Pipeline

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!"
        }
    }
}
Code language: PHP (php)

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
    }
}
Code language: PHP (php)

Integer Variable:


node {
    stage('Example') {
        // Define an integer variable
        def myInteger = 10

        // Access and use the integer variable
        echo myInteger
    }
}
Code language: PHP (php)

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."
        }
    }
}
Code language: PHP (php)

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
        }
    }
}
Code language: PHP (php)

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}"
    }
}
Code language: PHP (php)

Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
rakesh
rakesh
2 years ago

thanks for sharing all types of variables Scripting pipeline that developer use regularly that helps lot

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

1
0
Would love your thoughts, please comment.x
()
x