1
votes

I have a jenkins pipeline which load a groovy utility script like this :

Utils = load('/var/lib/jenkins/utils/Utils.groovy')

Everything is fine when I execute the pipeline on the master node. In this case I'm able to use the methods inside my class Utils in the pipeline.

node('master'){
    stage('stage1'){
        def Utils = load('/var/lib/jenkins/utils/Utils.groovy')
        Utils.doSomething()
    }
}

My problem came when I try to execute my pipeline in a slave. In this case the load above causes the error

java.io.IOException: java.io.FileNotFoundException: /var/lib/jenkins/utils/Utils.groovy (No such file or directory)

To avoid this error, in the pipeline, I load the file in master node like this

node('master'){
    stage('stage1'){
        Utils = load('/var/lib/jenkins/utils/Utils.groovy')
    }
}
node(){
    stage('stage2'){
        Utils.doSomething()
    }
}

This is not very efficient and I don't want to use the master just for loading the file Have you any advice on how to load a Groovy scipt on a slave node ?

Thank you

1

1 Answers

-1
votes

First, the above error java.io.IOException: java.io.FileNotFoundException: /var/lib/jenkins/utils/Utils.groovy (No such file or directory) was caused when you tried to load the file when executing on the slave node. But the file is stored in /var/lib/jenkins/utils/Utils.groovy in your master node, which is another computer and another file system, I guess. So, the mistake is logical.

When you execute some pipeline operations on another node (computer, server etc.), and you want to load the file, you need to have it stored in that computer(slave) and load it from there - so the path have to be according to file place in slave computer.

So, I would suggest to:

  1. simply store Utils.groovy file on slave machine and load it from there
  2. load it to your workspace on master(as you did already)
  3. Also, you can store groovy file code in github and load it from there not depending on master/slave filesystems (I would advice this option)