0

I have the following stages in my test.groocy pipeline:

stages { stage('Test') { steps { env.SOME_VAR = new File("settings.gradle") .readLines() .findAll { someLogic } .collect { it =~ /.*'(.*)'/ } echo "got: ${SOME_VAR}" } } } 

But I'm getting:

java.io.FileNotFoundException: settings.gradle (No such file or directory) 

If I change the code to:

stages { stage('Test') { steps { sh(returnStdout: true, script: '''#!/bin/bash cat settings.gradle''') } } } 

I can see the file's content.

  1. Why isn't it recognizing the file in the first snippet, but does recognize it in the second one?
  2. Is there a way to make the upper snippet works as expected in Jenkins?
1
  • 1
    First guess would be because the File class only works correctly for paths on the Jenkins master, and not the build agents. Is this file on the master? Commented Nov 4, 2020 at 15:27

1 Answer 1

1

sh, as other Jenkins steps, started in current workspace directory.

So, you could replace native java/groovy new File with Jenkins readFile and the rest of your code should work.

readFile("settings.gradle").readLines()... 

Or specify the full path for new File

new File("${WORKSPACE}/settings.gradle").readLines()... 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did it.
groovy (actually java) works in global jenkins agent environment. and i believe current dir for jenkins would be jenkins/bin (or smth like this). you can check it by: println( new File(".").getAbsolutePath() ). for all jobs the value would be the same.
That's very helpful. Thanks for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.