rkkreddy rkkreddy - 1 year ago 374
Groovy Question

How to re-use groovy script in Jenkins Groovy Post Build plugin?

I have some groovy code which I am planning to re-use in Jenkins Groovy Post Build plugin of multiple jobs. How can I achieve this? Is there a place I can store the script in a global variable and call that in the jobs where ever I need?

Answer Source

You can load any groovy file living on the Jenkins master within the groovy postbuild and execute it. For example, you could have a special directory on the c drive where all the common scripts live. I'll update my answer later with some code that shows you how to load the script in.


Assuming you have a test.groovy file on your C: drive, it should be as simple as the following in Groovy Postbuild:

evaluate(new File("C:\\test.groovy"))

Please view the comment section of the Groovy Postbuild for more examples and possibly other ways.