How do you load a groovy file and execute it

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()
}

Leave a Comment