我正在尝试使用 java.nio.file.* 中的方法在 Jenkins 管道中执行一些基本文件操作。无论代码存在于哪个节点 block ,代码都在主节点上执行。在管道中,我已经验证了各种节点 block 是正确的——它们唯一地标识特定节点。但是,pathExists(以及其他移动、复制或删除文件的代码)始终在主节点上执行。知道发生了什么或如何解决它吗?
import java.nio.file.*
String slavePath = 'C:\\Something\\only\\on\\slave\\node'
String masterPath = 'D:\\Something\\only\\on\\master\\node'
def pathExists (String pathName)
{
def myPath = new File(pathName)
return (myPath.exists())
}
stage('One')
{
node ('slave')
{
bat returnStatus: true, script: 'set'
println (pathExists(slavePath)) // Should be true but is false.
println (pathExists(masterPath)) // Should be false but is true.
}
node ('master')
{
bat returnStatus: true, script: 'set'
println (pathExists(slavePath)) // false
println (pathExists(masterPath)) // true
}
}
最佳答案
这是管道脚本的规范。它写在 tutorial 中.
readFile
step loads a text file from the workspace and returns its content (do not try to usejava.io.File
methods — these will refer to files on the master where Jenkins is running, not in the current workspace).There is also a
writeFile
step to save content to a text file in the workspace
fileExists
step to check whether a file exists without loading it.
您可以在节点中使用这些 Jenkins 步骤,而不是 java.io.File
或 java.nio.file.Files
,如下所示。
String slavePath = 'C:\\Something\\only\\on\\slave\\node'
String masterPath = 'D:\\Something\\only\\on\\master\\node'
stage('One')
{
node ('slave')
{
bat returnStatus: true, script: 'set'
println fileExists(slavePath) // Should be true
println fileExists(masterPath) // Should be false
}
node ('master')
{
bat returnStatus: true, script: 'set'
println fileExists(slavePath) // false
println fileExists(masterPath) // true
}
}
关于java - Jenkins 管道和 java.nio.file.* 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39753737/