admin管理员组文章数量:1356950
I have the following test code to try to open and modify the contents of a file:
private def updateTagInfoInFile(final String file) {
final String tmpFile = "tmp.txt"
sh "ls -al /data/workspace/Utilities/Playground"
sh "whoami; pwd; ls -al"
new File("/data/workspace/Utilities/Playground/copy-artifact/$file").eachLine{line->
if (line.startsWith("Tag")) {
// Count the number of spaces between "Tag: <tag>" to preserve the formatting
final Integer numSpaces = line.length() - line.replaceAll(" ", "").length()
line = "Tag:"+" ".multiply(numSpaces)+"newTag"
}
sh "echo '$line' >> $tmpFile"
}
sh "mv $tmpFile $file"
}
updateTagInfoInFile("someFile.txt")
But I get the following error:
+ ls -al /data/workspace/Utilities/Playground
total 0
drwxrwxr-x 4 ubuntu ubuntu 52 Mar 29 16:46 .
drwxrwxr-x 5 ubuntu ubuntu 136 Mar 29 16:38 ..
drwxrwxr-x 2 ubuntu ubuntu 45 Mar 29 16:38 copy-artifact
drwxrwxr-x 3 ubuntu ubuntu 30 Mar 29 17:29 copy-artifact@tmp
+ whoami
ubuntu
+ pwd
/data/workspace/Utilities/Playground/copy-artifact
+ ls -al
total 4
drwxrwxr-x 2 ubuntu ubuntu 45 Mar 29 16:38 .
drwxrwxr-x 4 ubuntu ubuntu 52 Mar 29 16:46 ..
-rw-rw-r-- 1 ubuntu ubuntu 2368 Mar 29 16:24 someFile.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Also: .jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: b41cce20-1d11-416e-8e89-e5aec9147ea5
java.io.FileNotFoundException: /data/workspace/Utilities/Playground/copy-artifact/someFile.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:78)
at .codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1599)
...
The file is obviously there, so why am I getting the error?
I have the following test code to try to open and modify the contents of a file:
private def updateTagInfoInFile(final String file) {
final String tmpFile = "tmp.txt"
sh "ls -al /data/workspace/Utilities/Playground"
sh "whoami; pwd; ls -al"
new File("/data/workspace/Utilities/Playground/copy-artifact/$file").eachLine{line->
if (line.startsWith("Tag")) {
// Count the number of spaces between "Tag: <tag>" to preserve the formatting
final Integer numSpaces = line.length() - line.replaceAll(" ", "").length()
line = "Tag:"+" ".multiply(numSpaces)+"newTag"
}
sh "echo '$line' >> $tmpFile"
}
sh "mv $tmpFile $file"
}
updateTagInfoInFile("someFile.txt")
But I get the following error:
+ ls -al /data/workspace/Utilities/Playground
total 0
drwxrwxr-x 4 ubuntu ubuntu 52 Mar 29 16:46 .
drwxrwxr-x 5 ubuntu ubuntu 136 Mar 29 16:38 ..
drwxrwxr-x 2 ubuntu ubuntu 45 Mar 29 16:38 copy-artifact
drwxrwxr-x 3 ubuntu ubuntu 30 Mar 29 17:29 copy-artifact@tmp
+ whoami
ubuntu
+ pwd
/data/workspace/Utilities/Playground/copy-artifact
+ ls -al
total 4
drwxrwxr-x 2 ubuntu ubuntu 45 Mar 29 16:38 .
drwxrwxr-x 4 ubuntu ubuntu 52 Mar 29 16:46 ..
-rw-rw-r-- 1 ubuntu ubuntu 2368 Mar 29 16:24 someFile.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Also: .jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: b41cce20-1d11-416e-8e89-e5aec9147ea5
java.io.FileNotFoundException: /data/workspace/Utilities/Playground/copy-artifact/someFile.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:78)
at .codehaus.groovy.runtime.ResourceGroovyMethods.newReader(ResourceGroovyMethods.java:1599)
...
The file is obviously there, so why am I getting the error?
Share Improve this question edited Apr 1 at 0:31 Chris F asked Mar 29 at 17:02 Chris FChris F 16.9k37 gold badges121 silver badges228 bronze badges 4 |1 Answer
Reset to default 1This is a common pitfall that I have also encountered myself in the past. The File
class performs operations on the Jenkins master, and not necessarily the build agents (unless the agent is the master
). The following line of code (which is definitely throwing the Exception
):
new File("/data/workspace/Utilities/Playground/copy-artifact/$file")
should probably be replaced with:
import hudson.FilePath
import jenkins.model.Jenkins
new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), "/data/workspace/Utilities/Playground/copy-artifact/$file")
However, I am not completely sure that code will perfectly replace the functionality you are currently seeking, and I believe I adapted it from an answer I saw years ago by someone more expert in Pipeline than myself. There may still be an issue here, and we can discuss it in the comments if necessary.
EDIT for full solution
I added the necessary functions to make Matthew's solution work for me.
new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), "/data/workspace/Utilities/Playground/copy-artifact/$file").readToString().split("\n").each{line->
// same code as above
}
本文标签: groovyJenkins pipeline cannot open an existing fileStack Overflow
版权声明:本文标题:groovy - Jenkins pipeline cannot open an existing file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744010643a2575498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
whoami
command and its output. It's clear userubuntu
has access to the file. – Chris F Commented Mar 29 at 17:22ls /data/workspace/Utilities/Playground
? – Lajos Arpad Commented Mar 29 at 17:26