In my case new File()
doesn't work, it causes a FileNotFoundException
when run in a Jenkins pipeline job. The following code solved this, and is even easier in my opinion:
def fileContents = readFile "path/to/file"
I still don't understand this difference completely, but maybe it'll help anyone else with the same trouble. Possibly the exception was caused because new File()
creates a file on the system which executes the groovy code, which was a different system than the one that contains the file I wanted to read.