jenkins pipeline functions

Jenkins pipeline functions | Complete tutorial in [2022]

This is the sixth part of the Jenkins pipeline example. This blog will understand Jenkins Pipeline functions and how to use a function inside a Jenkins pipeline.

To follow along. I assume that Jenkins is installed in your system. if not, please follow this link to quickly install Jenkins in your environment.

Jenkins pipeline function example

Functions are pretty powerful when it comes to performing repetitive operations. Jenkins also allows us to write and use functions with the Jenkins pipeline. This blog will understand how to use functions within the Jenkins pipeline.

In Jenkins, users can define the pipeline in two ways. Using the old-fashioned declarative syntax or using the recent and most popular declarative syntax.

You can check my blog on Jenkins pipeline syntax to have a basic understanding of the pipeline types. In the coming session, we will be using these two syntaxes to define functions in the Jenkins pipeline.

Jenkins scripted pipeline function

In this session, we will create a Jenkins scripted pipeline with functions using the groovy script. You can check my blog on Jenkins scripted pipeline to better understand the scripted pipeline syntax.

To create a function in the Jenkins pipeline with groovy, refer to the below code snippet.

def Greet(name) {
    echo "Hello ${name}"
}
node {
    stage('Hello') {
        Greet('NaiveSkill')
    }
}

Code Explanation

  • Initially, we have defined a function named Greet and which takes a parameter as the name.
  • In the next step, we create a Jenkins scripted pipeline that should start with a node block.
  • After the node block, stage is defined with the name “Hello” and inside that Hello stage, we call the name function and pass NaiveSkill as a parameter.

Click on the save button to save the pipeline. Now go to the Build now button to run the pipeline. Once the pipeline runs successfully, you will get the below output in logs.

Jenkins pipeline functions logs

Jenkins declarative pipeline functions

In the above example, we have a function in the scripted pipeline, but a user can also use the functions in the declarative pipeline. Refer to the below code to create a Jenkins declarative pipeline function using a groovy script.

def Greet(name) {
    echo "Hello ${name}"
}

pipeline {
    agent any
 
    stages {
        stage('Hello') {
            steps {
                Greet('NaiveSkill')
            }
        }
    }
}

Code explanation

  • Initially, a greet function is defined which prints Hello ${name}
  • After that, we have actually defined a Jenkins pipeline and selected any as agent.
  • This pipeline has a single hello stage which is defined under the stages block.
  • Finally, in the steps block, we are calling the Greet function and passing “NaiveSkill” as input.

Paste the above code in the Jenkins script session and click on the build now button. You will get the below output for the above code.

Jenkins declarative pipeline functions

Jenkins pipeline function return value

In the above example, we have seen how to use the function in the Jenkins pipeline which does not return any value. In this session, we will understand how to write a Jenkins pipeline function that will return any value.

The Jenkins pipeline which will return value can be defined as

def mul()
{
   def a=2
   def b=3
   def mul=a*b
   return mul
}

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps{
                script{
                  output= mul()
                  echo "The mul is ${output}"
            }
            }
        }
    }
    }

Code explanation

  • Using the def method we have defined a mul function that will take two numbers and return the multiplication of it.
  • Next, we have to follow the usual pipeline block syntax which includes stages, stage, and steps block.
  • In the step block, we have to define the script block to get the mul() function output in an output variable and finally use the echo method to print the mul() result.

Save the above code in a Jenkins pipeline and click on the build now button. You will get the below output on a successful job run.

Jenkins pipeline function return value

Jenkins scripted pipeline function return value

In this session, we will use the scripted pipeline to define a function in the Jenkins pipeline that returns a value. The Jenkins scripted pipeline that will return value can be defined as

def add() {
   def a=2
   def b=3
   def add=a+b
   return add
}
node {
    stage('Hello') {
           output = add()
           echo "The Addition of two numbers a and b is ${output}"
    }
}

Code explanation

  • Using the def method an addition function is defined that will take two numbers and return the addition of it.
  • Next, we need to use the node and stage block to define the scripted pipeline.
  • In the stages block, we take the function output into an output variable, and finally using the echo command we are printing out the result.
  • Please make a note that in the scripted pipeline there is no need to define the script block as the scripted pipeline automatically executes the scripts written in groovy format.

Save the pipeline and click on build now. If you are getting below output means your job is working as expected

Jenkins scripted pipeline function return value

Conclusion

I hope you like this tutorial on using functions in the Jenkins pipeline. In this blog, we have understood how to define a function in Jenkins declarative and scripted pipeline. We have also learned how to write a Jenkins pipeline function that returns a value.

Please do let me know in the comment box if you are facing any issues while following along.

More to Explore?

Jenkins workflow

Jenkins pipeline example hello-world

Jenkins pipeline example multiple stages

Jenkins pipeline define a variable

Leave a Comment

Your email address will not be published.

Scroll to Top