Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you load a groovy file and execute it

I have a jenkinsfile dropped into the root of my project and would like to pull in a groovy file for my pipeline and execute it. The only way that I've been able to get this to work is to create a separate project and use the fileLoader.fromGit command. I would like to do

def pipeline = load 'groovy-file-name.groovy' pipeline.pipeline() 
like image 985
user301693 Avatar asked Jun 13 '16 22:06

user301693


People also ask

How do I run a Groovy file in Jenkins?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name.

How do I load Groovy in Jenkins pipeline?

In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.

Is a Jenkins file a Groovy file?

The Jenkins file is a base code for Jenkins which executes it as a Groovy script in Jenkins script console.


1 Answers

If your Jenkinsfile and groovy file in one repository and Jenkinsfile is loaded from SCM you have to do:

Example.Groovy

def exampleMethod() {     //do something }  def otherExampleMethod() {     //do something else } return this 

JenkinsFile

node {     def rootDir = pwd()     def exampleModule = load "${rootDir}@script/Example.Groovy "     exampleModule.exampleMethod()     exampleModule.otherExampleMethod() } 
like image 176
Anton Shishkin Avatar answered Sep 18 '22 08:09

Anton Shishkin