java - Jenkins 管道和 java.nio.file.* 方法的问题

标签 java jenkins groovy jenkins-pipeline java.nio.file

我正在尝试使用 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 use java.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.Filejava.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/

相关文章:

java - 从文本字段获取用户输入并将其添加到 GUI 列表中显示的数组列表中

Java以正确的形式输出矩阵

java - 在 IZPack 中自定义安装程序

jenkins - 在 Jenkins 管道中获取步骤 ID 以链接到 BlueOcean 或管道步骤 View (flowGraphTable)

linux - 配置 Jenkins 在构建时通过构建参数以编程方式确定从属?

Java使用lastindexof从文件列表中提取日期

linux - 从 Jenkins 中运行的脚本通过远程 ssh 将文件内容分配给变量

jenkins - sed 在 jenkins 上失败,但在本地工作

java - 在 Groovy 中使用递归嵌套映射合并映射

javascript - SOAPUI:SIMple Groovy 脚本 - 导入语句存在语法错误?