Issue
I am trying to read a file in a jenkins pipeline. I create this file using Linux 'tee' command. I try to read the file using in built java functions such as Files.readAllLines in java.nio or BufferedReader in java.io package. None of the either 2 cases work.
The reason I want to use either of these 2 or a similar technique is because I have to read this file in a shared library and not in Jenkins file. I try to read the file in a Jenkins pipeline so as to test whether these 2 methods work or not.
How can I use these techniques to read already existing file in my workspace ?
Also, I tried to use jenkins readFile method and it works but I think I cannot use it in my shared library.
My Jenkins file is:
import java.io.file.*
pipeline {
agent any
stages {
stage('Scan Timeout Test') {
steps {
script {
sh '''echo "Running ls before ..."
ls -lt
'''
sh 'ls -lt | tee lslog.txt'
sh '''pwd
ls -lt
'''
getLogs("$WORKSPACE/lslog.txt")
}
}
} //end of stage
}
}
def getLogs(logPath) throws IOException {
//println "Reading $logPath..."
//def text = readFile logPath
//println text
/*println "[INFO] Reading Log File: " + Paths.get(logPath).toAbsolutePath().toString()
try {
return Files.readAllLines(Paths.get(logPath), StandardCharsets.UTF_8);
} catch(IOException e){
println "[ERROR] Failed to read file '"+logPath+"': "+e.getMessage()
throw e
}*/
BufferedReader bufReader = new BufferedReader(new FileReader(logPath));
ArrayList<String> listOfLines = new ArrayList<>();
String line = bufReader.readLine();
while (line != null) {
listOfLines.add(line);
line = bufReader.readLine();
}
bufReader.close();
return listOfLines
}
The output that I get in my Jenkins console is:
+ pwd
/jenkins/workspace/test
+ ls -lt
total 44
-rw-r--r-- 1 root root 526 Aug 21 11:32 lslog.txt
drwxr-xr-x 2 root root 173 Aug 21 11:32 vars
drwxr-xr-x 3 root root 109 Aug 21 11:32 resources
drwxr-xr-x 4 root root 29 Aug 21 11:32 src
-rw-r--r-- 1 root root 22783 Aug 21 11:32 README.md
-rw-r--r-- 1 root root 602 Aug 21 11:32 build.gradle
drwxr-xr-x 3 root root 21 Aug 21 11:32 gradle
-rw-r--r-- 1 root root 5296 Aug 21 11:32 gradlew
-rw-r--r-- 1 root root 2176 Aug 21 11:32 gradlew.bat
drwxr-xr-x 2 root root 118 Aug 21 11:32 integration-tests
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.FileNotFoundException: /jenkins/workspace/test/lslog.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
Using ls -lt, I can see that my file, lslog.txt is created in the directory but I cannot read it.
Solution
You can use readFile in shared libraries as long as you pass the steps along. Check the documentation at https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps
package org.foo
class bar {
static void readFile(filePath, steps) {
def text = steps.readFile(file: filePath)
}
}
Answered By - Steve
Answer Checked By - Clifford M. (JavaFixing Volunteer)