我正在尝试使用 java.nio.file.* 中的方法在 Jenkins 管道中执行一些基本文件操作。无论代码存在于哪个节点块中,代码都在主节点上执行。在管道中,我已经验证了各个节点块都是正确的——它们唯一地标识了特定的节点。但是,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 https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#adding-more-complex-logic.
readFile
步骤从工作区加载文本文件并返回其
内容(不要尝试使用java.io.File
方法——这些将指
文件位于 Jenkins 运行的 master 上,而不是当前的
工作区).
还有一个writeFile
步骤将内容保存到文本文件中
工作区
fileExists
步骤检查文件是否存在而不加载它。
您可以在节点中使用这些 Jenkins 步骤,而不是java.io.File
or 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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)