Home > Back-end >  Jenkins groovy file can't import another groovy
Jenkins groovy file can't import another groovy

Time:04-17

In simple form, I have 2 groovy files in the repo in /jenkins subfolder. File A.groovy and B.groovy. Inside A.groovy I have a load line

load(env.WORKSPACE   "@script/jenkins/B.groovy")

The problem is I get an error

java.nio.file.NoSuchFileException:
/Users/user/.jenkins/workspace/JobName@script/jenkins/B.groovy

But as we see, overall looks like load function created kinda almost correct url. The point is my actual fetched repo, and particularly A.groovy is getting into the additional subfolder. I see that in the very beginning of the logs and can find locally there.

Checking out git ... into /Users/user/.jenkins/workspace/JobName@script/ecb7a9317b1ad672698830264d9e0ce2b9b6f330c043bb85f48623f3cdcab65e/jenkins/A.groovy

Tried to log whole env object using echo sh(script: 'env|sort', returnStdout: true) and there is no any property containing that subfolder name at all.

Why I am getting that extra ecb7a9317b1ad672693830224d9e0ce2b9b3f730c043bb85f48925f3cdcab65e subfolder and how can I either get rid of it or get it's name somehow to compose correct url for import?

CodePudding user response:

Putting this as an answer since its too long to add as a comment, although this might not directly answer your problem. The characters were added to fix this issue, and a lot of people are facing problems with it, which is why another issue was raised. If you go through the comments of both issues, you will find many workarounds suggested by the community. There are several plugins made to solve the problem and I'm led to believe that the issue is actually fixed on the latest version on Jenkins.

CodePudding user response:

I've found a workaround (by searching the folder), but would like to find a better and native way.

Where jenkins in -name 'jenkins' is the subfolder name containing groovy scripts.

git_jenkins_folder = sh (
        script: "find \""   WORKSPACE   "\"@script -type d -name 'jenkins'",
        returnStdout: true
    ).trim()

utils = load("$git_jenkins_folder/Utils.groovy")
  • Related