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

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

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文件内容如下:

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")
                }
            }
        }
    }
}

def getLogs(logPath) throws IOException {
    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
}

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

+ 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
...
java.io.FileNotFoundException: /jenkins/workspace/test/lslog.txt (No such file or directory)
...

使用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:

import java.io.file.*

pipeline {
agent any

stages {
    stage(&#39;Scan Timeout Test&#39;) {
        steps {
            script {
				sh &#39;&#39;&#39;echo &quot;Running ls before ...&quot;
ls -lt
				&#39;&#39;&#39;
                sh &#39;ls -lt | tee lslog.txt&#39;
				sh &#39;&#39;&#39;pwd
				ls -lt
				&#39;&#39;&#39;
				getLogs(&quot;$WORKSPACE/lslog.txt&quot;)
            }
        }
    } //end of stage
  }
}

def getLogs(logPath) throws IOException {
    //println &quot;Reading $logPath...&quot;
    //def text = readFile logPath
    //println text
     /*println &quot;[INFO] Reading Log File: &quot; + Paths.get(logPath).toAbsolutePath().toString()
     try {
       return Files.readAllLines(Paths.get(logPath), StandardCharsets.UTF_8);
     } catch(IOException e){
         println &quot;[ERROR] Failed to read file &#39;&quot;+logPath+&quot;&#39;: &quot;+e.getMessage()
         throw e
     }*/
     BufferedReader bufReader = new BufferedReader(new FileReader(logPath)); 
     ArrayList&lt;String&gt; listOfLines = new ArrayList&lt;&gt;(); 
 
     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.

答案1

得分: 3

可以在共享库中使用readFile,只要你将步骤传递下去。
请查阅文档: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)
  }
}
英文:

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)
  }
}

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:

确定