opike
opike

Reputation: 7585

Attempting to access file system from jenkinsfile

I'm trying to access the file system from a Jenkinsfile using groovy. I'm following the suggestion in this SO thread: Recursive listing of all files matching a certain filetype in Groovy

I've already given access to

new java.io.File java.lang.String
staticField groovy.io.FileType FILES

under Script Approvals.

However this line is failing without an error message:

new File('.').eachFileRecurse(FILES) {

Here's the broader code block in question:

stage("Install") {
  print "here a"
  new File('.').eachFileRecurse(FILES) {
    print "here c"
    if(it.name.endsWith(".sh")) {
      print "here d"
      println it
    }
  }
  print "here b"

Here's the console output from that section:

[Pipeline] stage
[Pipeline] { (Install)
[Pipeline] echo
here a
[Pipeline] }
[Pipeline] // stage
[Pipeline] echo
Email Recipients: [email protected], [email protected]
[Pipeline] emailext
messageContentType = text/html; charset=UTF-8
Adding recipients from project recipient list
Adding recipients from trigger recipient list
Setting In-Reply-To since last build was not successful

Upvotes: 2

Views: 3221

Answers (1)

StephenKing
StephenKing

Reputation: 37620

Better use the findFiles step, instead of the Java API to read files. This is whitelisted by script security.

each loops don't work in pipeline because of Jenkins' CPS. Instead (if you really prefer it over good old for loops), wrap it in a method annotated with @NonCPS.

Upvotes: 2

Related Questions