6
我想用java.nio.file。*中的方法在Jenkins管道中执行一些基本的文件操作。无论存在代码的节点块如何,代码都会在主节点上执行。在流水线中,我已经验证了各种节点块是正确的 - 它们唯一地标识特定的节点。但是,pathExists(以及移动,复制或删除文件的其他代码)始终在主节点上执行。任何想法发生了什么或如何解决它?Jenkins管道和java.nio.file。*方法问题
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
}
}
我错过了在文档中。因此,如果我理解正确,在Windows上的工作空间内部和外部的主节点和从节点上使用单一方法会涉及使用Groovy运行bat文件来返回答案,可能通过运行PowerShell Test-File? – gaccardo
嗯我没有明白你的意思。是不是与java.io.File问题无关的另一个问题?我在答案中增加了一个例子,它会按照您的预期运行以防万一。 – arasio
根据[文档](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-fileexists-code-verify-if-file-exists-in-workspace),仅fileExists适用于作业工作区中的文件。当我最初尝试时,情况就是如此。即使文件不在工作区中,运行示例也能正常工作。我已经用Jenkins 2.24和2.28验证了这一点。 – gaccardo