How do you load a groovy file and execute it

JenkinsGroovyJenkins Pipeline

Jenkins Problem Overview


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

Jenkins Solutions


Solution 1 - Jenkins

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

Solution 2 - Jenkins

If you have pipeline which loads more than one groovy file and those groovy files also share things among themselves:

JenkinsFile.groovy

def modules = [:]
pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                script{
                    modules.first = load "first.groovy"
                    modules.second = load "second.groovy"
                    modules.second.init(modules.first)
                    modules.first.test1()
                    modules.second.test2()
                }
            }
        }
    }
}

first.groovy

def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this

second.groovy

import groovy.transform.Field
@Field private First = null

def init(first) {
    First = first
}
def test1(){
    //add code for this method
}
def test2(){
    First.test2()
}
return this

Solution 3 - Jenkins

You have to do checkout scm (or some other way of checkouting code from SCM) before doing load.

Solution 4 - Jenkins

Thanks @anton and @Krzysztof Krasori, It worked fine if I combined checkout scm and exact source file

Example.Groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this

JenkinsFile

node {
    // Git checkout before load source the file
    checkout scm
    
    // To know files are checked out or not
    sh '''
        ls -lhrt
    '''

    def rootDir = pwd()
    println("Current Directory: " + rootDir)

    // point to exact source file
    def example = load "${rootDir}/Example.Groovy"

    example.exampleMethod()
    example.otherExampleMethod()
}

Solution 5 - Jenkins

Very useful thread, had the same problem, solved following you.

My problem was: Jenkinsfile -> call a first.groovy -> call second.groovy

Here my solution:

Jenkinsfile

node {
  checkout scm
  //other commands if you have
  
  def runner = load pwd() + '/first.groovy'
  runner.whateverMethod(arg1,arg2)
}

first.groovy

def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}

NB: args are optional, add them if you have or leave blank.

Hope this could helps further.

Solution 6 - Jenkins

In case the methods called on your loaded groovy script come with their own node blocks, you should not call those methods from within the node block loading the script. Otherwise you'd be blocking the outer node for no reason.

So, building on @Shishkin's answer, that could look like

Example.Groovy
def exampleMethod() {
    node {
        //do something
    }
}

def otherExampleMethod() {
    node {
        //do something else
    }
}
return this
Jenkinsfile
def exampleModule
node {
    checkout scm // could not get it running w/o checkout scm
    exampleModule = load "script/Example.Groovy"
}
exampleModule.exampleMethod()
exampleModule.otherExampleMethod()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser301693View Question on Stackoverflow
Solution 1 - JenkinsAnton ShishkinView Answer on Stackoverflow
Solution 2 - JenkinsawefsomeView Answer on Stackoverflow
Solution 3 - JenkinsKrzysztof KrasońView Answer on Stackoverflow
Solution 4 - JenkinsMallikarjunarao KosuriView Answer on Stackoverflow
Solution 5 - JenkinskancioView Answer on Stackoverflow
Solution 6 - JenkinsJoerg SView Answer on Stackoverflow