Shared library usage in pipelines, Multiple functions inside groovy script

Devops article 34:​​ Shared library usage in pipelines,​​ Multiple functions inside groovy script

What you will Learn:

  • Usage of shared libraries in​​ pipeline

  • Multiple functions inside groovy script

Usage of shared libraries in​​ pipeline

Jenkins shared libraries​​ can be​​ used for code re-usability in pipeline jobs.​​ Suppose you have a code logic being used in a pipeline job. If you want to use the same logic in another job, you can do so using shared libraries.

Let us see practically how to do that. The first step is to create a folder​​ vars​​ inside repository and create​​ greetings.groovy​​ file inside this folder as shown below. The ‘vars’ folder will have our groovy scripts​​ inside which we will be putting our reusable functions. You can name the file anything you want but file extension should be .groovy

​​ 

So,​​ greetings.groovy​​ will be our shared library.​​ 

Let us define a function​​ ‘call()’​​ in this file​​ which accepts 1 argument​​ ‘name’​​ as shown below

Important note: Make sure that the function name is call() otherwise you will get error later. The reason being, groovy calls the​​ call()​​ function​​ by default.​​ 

Let us now write line#3 to print out something

Commit this file

Go one directory backwards as shown below and copy the url

Next in Jenkins dashboard, go to ‘Manage Jenkins > click Configure System’

Scroll to the page bottom, you would find ‘Global Pipeline Libraries’

Click Add​​ and give some library name

Next, since our ‘vars’ folder is inside the ‘main’ branch, we will mention ‘main’ in the ‘Default version’ field​​ 

Next, we see a section ‘Retrieval method’ where we define how to retrieve shared libraries.​​ The repository url that you had copied above, paste it in the ‘Project Repository’ field as shown below

Leave remaining fields as they are.​​ Note down the library name that we mentioned above

Click Save.

Create a new pipeline job.

To use the functions of library, we have to​​ first​​ import the library. The syntax is​​ @Library(‘<Library name>’) _​​ as​​ shown in line#1​​ below

We then write the pipeline structure as shown below

Now we will call the shared library. The groovy function name inside shared library is ‘greetings​​ that takes 1 argument

So we call this groovy function as shown below and pass an argument

Apply/Save/Build.

See the logs and notice the output as shown below. The ${name} is being replaced by Testuser1

The entire code is:

@Library('SharedLib') _

pipeline

{

 ​​ ​​ ​​​​ agent any

 ​​ ​​ ​​​​ stages

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ stage('Demonstration of shared library')

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ steps

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ greetings("Testuser1")

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

You can similarly use the same library in another pipeline job.

Let us configure the above job and call the function multiple times as shown below

Apply/Save/Build. Notice that the o/p gets printed 3 times as shown below

Multiple functions inside groovy script

Let us add one for groovy script file inside ‘vars’ folder, but this time let us not have call() function

Commit this file.

Configure the same job and add below 2 lines. This time we have to call the respective function name (add or sub) unlike earlier where we did not call the call() function

Apply/Save/Build.

The build will fail​​ as shown below

The reason is​​ math.sub(10,7)​​ is groovy syntax. In declarative pipeline, we should not use groovy syntax​​ directly.​​ We should wrap the groovy syntax inside the ‘script’ block as mentioned in the above error.

Configure the same job​​ and wrap the 2 lines under script{} block

Apply/Save/Build.

This time the build is successful as shown below

This is how we can use shared libraries in pipeline jobs.

Complete code:

@Library('SharedLib') _

pipeline

{

 ​​ ​​ ​​​​ agent any

 ​​ ​​ ​​​​ stages

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ stage('Demonstration of shared library')

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ steps

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ greetings("Testuser1")

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ greetings("Testuser2")

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ greetings("Testuser3")

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ script

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ math.sub(10,7)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ math.add(5,15)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024