问题与Jenkins管道和java.nio.file。*方法 [英] Issue with Jenkins pipeline and java.nio.file.* methods
问题描述
我试图使用java.nio.file。*中的方法在Jenkins管道中执行一些基本的文件操作。无论存在代码的节点块如何,代码都会在主节点上执行。在流水线中,我已经验证了各种节点块是正确的 - 它们唯一地标识特定的节点。但是,pathExists(以及移动,复制或删除文件的其他代码)始终在主节点上执行。任何想法发生了什么或如何解决它?
import java.nio.file。*
String slavePath ='C:\\Something\\\\\\\\\\\\\\\\\\\\\\\\\\' \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' b $ b return(myPath.exists())
}
stage('One')
{
node('slave')
{
bat returnStatus:true,script:'set'
println(pathExists(slavePath))//应该为true,但为false。
println(pathExists(masterPath))//应该是false,但是是true。
}
node('master')
{
bat returnStatus:true,script:'set'
println(pathExists(slavePath))// false
println(pathExists(masterPath))// true
}
}
这是管道脚本的规范。它写在教程中。
readFile
文件并返回它的
内容(不要试图使用java.io.File
方法 - 这些将引用
文件在Jenkins正在运行的主服务器上,而不是在当前的
工作区中)。writeFile
将内容保存到
工作区中的文本文件中
- $ / $>
fileExists
步骤来检查文件是否存在而不加载。您可以在节点中使用这些Jenkins步骤而不是 java.io.File
或 java.nio.file.Files $ c $
String slavePath ='C:\\Something\\only\\on \\\\\\节点'
String masterPath ='D:\\\\\\\\\\\\\'节点'
stage'('One')
{
node('slave')
{
bat returnStatus:true,script:'set'
println fileExists(slavePath)/ /应该是真的
println fileExists(masterPath)//应该是false
node('master')
{
bat returnStatus:true,script:'设置'
println fileExists(slavePath)// false
println fileExists(masterPath)// true
}
}
I am trying to use methods from java.nio.file.* to perform some basic file operations in a Jenkins pipeline. Regardless of the node block in which the code exists, the code executes on the master node. In the pipeline, I have verified that the various node blocks are correct--they uniquely identify specific nodes. However, pathExists (and other code that moves, copies, or deletes files) always executes on the master node. Any ideas what's happening or how to fix it?
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
}
}
This is a specification of pipeline script. It's written in the 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.
You can use those Jenkins steps in a node instead of java.io.File
or java.nio.file.Files
as below.
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
}
}
这篇关于问题与Jenkins管道和java.nio.file。*方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!