无法读取Jenkins目录中的文件。

huangapple go评论116阅读模式
英文:

Not able to read a file present in the directory in Jenkins

问题

我正尝试在Jenkins流水线中读取一个文件。我使用Linux的'tee'命令创建了这个文件。
我尝试使用内置的Java函数来读取文件,比如java.nio中的Files.readAllLines或java.io包中的BufferedReader。
这两种情况都不起作用。

我想要使用这两种方法或类似的技术的原因是因为我必须在共享库中而不是在Jenkins文件中读取这个文件。
我试图在Jenkins流水线中读取文件,以测试这两种方法是否有效。

如何使用这些技术来读取工作区中已存在的文件?

另外,我尝试使用了Jenkins的readFile方法,它有效,但我认为我不能在我的共享库中使用它。

我的Jenkins文件内容如下:

  1. import java.io.file.*
  2. pipeline {
  3. agent any
  4. stages {
  5. stage('Scan Timeout Test') {
  6. steps {
  7. script {
  8. sh '''echo "Running ls before ..."
  9. ls -lt
  10. '''
  11. sh 'ls -lt | tee lslog.txt'
  12. sh '''pwd
  13. ls -lt
  14. '''
  15. getLogs("$WORKSPACE/lslog.txt")
  16. }
  17. }
  18. }
  19. }
  20. }
  21. def getLogs(logPath) throws IOException {
  22. BufferedReader bufReader = new BufferedReader(new FileReader(logPath));
  23. ArrayList<String> listOfLines = new ArrayList<>();
  24. String line = bufReader.readLine();
  25. while (line != null) {
  26. listOfLines.add(line);
  27. line = bufReader.readLine();
  28. }
  29. bufReader.close();
  30. return listOfLines
  31. }

在我的Jenkins控制台中,我得到的输出是:

  1. + pwd
  2. /jenkins/workspace/test
  3. + ls -lt
  4. total 44
  5. -rw-r--r-- 1 root root 526 Aug 21 11:32 lslog.txt
  6. drwxr-xr-x 2 root root 173 Aug 21 11:32 vars
  7. drwxr-xr-x 3 root root 109 Aug 21 11:32 resources
  8. ...
  9. java.io.FileNotFoundException: /jenkins/workspace/test/lslog.txt (No such file or directory)
  10. ...

使用ls -lt命令,我可以看到我的文件lslog.txt在目录中被创建,但我无法读取它。

英文:

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:

  1. import java.io.file.*
  2. pipeline {
  3. agent any
  4. stages {
  5. stage(&#39;Scan Timeout Test&#39;) {
  6. steps {
  7. script {
  8. sh &#39;&#39;&#39;echo &quot;Running ls before ...&quot;
  9. ls -lt
  10. &#39;&#39;&#39;
  11. sh &#39;ls -lt | tee lslog.txt&#39;
  12. sh &#39;&#39;&#39;pwd
  13. ls -lt
  14. &#39;&#39;&#39;
  15. getLogs(&quot;$WORKSPACE/lslog.txt&quot;)
  16. }
  17. }
  18. } //end of stage
  19. }
  20. }
  21. def getLogs(logPath) throws IOException {
  22. //println &quot;Reading $logPath...&quot;
  23. //def text = readFile logPath
  24. //println text
  25. /*println &quot;[INFO] Reading Log File: &quot; + Paths.get(logPath).toAbsolutePath().toString()
  26. try {
  27. return Files.readAllLines(Paths.get(logPath), StandardCharsets.UTF_8);
  28. } catch(IOException e){
  29. println &quot;[ERROR] Failed to read file &#39;&quot;+logPath+&quot;&#39;: &quot;+e.getMessage()
  30. throw e
  31. }*/
  32. BufferedReader bufReader = new BufferedReader(new FileReader(logPath));
  33. ArrayList&lt;String&gt; listOfLines = new ArrayList&lt;&gt;();
  34. String line = bufReader.readLine();
  35. while (line != null) {
  36. listOfLines.add(line);
  37. line = bufReader.readLine();
  38. }
  39. bufReader.close();
  40. return listOfLines
  41. }
  42. The output that I get in my Jenkins console is:
  43. + pwd
  44. /jenkins/workspace/test
  45. + ls -lt
  46. total 44
  47. -rw-r--r-- 1 root root 526 Aug 21 11:32 lslog.txt
  48. drwxr-xr-x 2 root root 173 Aug 21 11:32 vars
  49. drwxr-xr-x 3 root root 109 Aug 21 11:32 resources
  50. drwxr-xr-x 4 root root 29 Aug 21 11:32 src
  51. -rw-r--r-- 1 root root 22783 Aug 21 11:32 README.md
  52. -rw-r--r-- 1 root root 602 Aug 21 11:32 build.gradle
  53. drwxr-xr-x 3 root root 21 Aug 21 11:32 gradle
  54. -rw-r--r-- 1 root root 5296 Aug 21 11:32 gradlew
  55. -rw-r--r-- 1 root root 2176 Aug 21 11:32 gradlew.bat
  56. drwxr-xr-x 2 root root 118 Aug 21 11:32 integration-tests
  57. [Pipeline] }
  58. [Pipeline] // script
  59. [Pipeline] }
  60. [Pipeline] // stage
  61. [Pipeline] }
  62. [Pipeline] // withEnv
  63. [Pipeline] }
  64. [Pipeline] // node
  65. [Pipeline] End of Pipeline
  66. java.io.FileNotFoundException: /jenkins/workspace/test/lslog.txt (No such file or directory)
  67. 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.

答案1

得分: 3

可以在共享库中使用readFile,只要你将步骤传递下去。
请查阅文档:https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps

  1. package org.foo
  2. class bar {
  3. static void readFile(filePath, steps) {
  4. def text = steps.readFile(file: filePath)
  5. }
  6. }
英文:

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

  1. package org.foo
  2. class bar {
  3. static void readFile(filePath, steps) {
  4. def text = steps.readFile(file: filePath)
  5. }
  6. }

huangapple
  • 本文由 发表于 2020年8月21日 19:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63522433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定