You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨平台Jenkins获取目录内容报错:路径混合问题排查与解决

问题原因与解决方案

代码运行节点确认

这段getFilesNamesFromDir函数是在Linux主节点上执行的。因为Groovy的new File()操作会直接调用执行代码所在节点的文件系统,而当前流水线没有将这段文件遍历逻辑绑定到Windows代理节点运行。主节点无法访问Windows代理的本地工作区路径,同时env.WORKSPACE在主节点和代理节点上的值不同,导致拼接出了混合Linux/Windows格式的错误路径/var/lib/jenkins/c:\jenkins\workspace\app-name_4.2.1/test,最终抛出文件未找到异常。

解决方法

方法1:使用Jenkins Pipeline内置的findFiles步骤(推荐)

Jenkins提供了跨节点兼容的文件操作步骤findFiles,它会自动在当前指定的agent节点执行,适配对应系统的路径格式,无需手动处理路径问题。

修改后的代码示例:

def getFilesNamesFromDir(dir) {
    // 使用findFiles获取文件列表,返回File对象数组
    def files = findFiles(glob: "${dir}/**")
    // 过滤出文件(排除目录)并提取文件名
    return files.findAll { !it.directory }.collect { it.name }
}

...

def testDir = "test" // 直接用相对路径,findFiles会基于当前agent的WORKSPACE

try {
    // 确保代码在Windows代理节点执行,替换为你的代理节点标签
    node('windows-agent') { 
        testResults = getFilesNamesFromDir(testDir)
    }
} catch (err) {
    println(err.toString());
    println(err.getMessage());
    err.printStackTrace();
    error "获取文件列表失败"
}

方法2:将Groovy文件操作逻辑限定在Windows代理节点执行

如果坚持使用Groovy的File类,必须确保代码在Windows代理节点上运行,通过node块指定代理节点,此时env.WORKSPACE会是Windows代理上的路径,文件操作才能正确访问本地文件系统。

修改后的代码示例:

def getFilesNamesFromDir(dir) {
    def fileNames = []
    new File(dir).eachFile() { file -> 
        if (!file.directory) { // 可选:过滤目录,只保留文件
            fileNames << file.getName()
        }
    }
    return fileNames
}

...

try {
    // 指定在Windows代理节点执行逻辑,替换为你的代理节点标签
    node('windows-agent') { 
        def testDir = "${env.WORKSPACE}/test"
        testResults = getFilesNamesFromDir(testDir)
    }
} catch (err) {
    println(err.toString());
    println(err.getMessage());
    err.printStackTrace();
    error "获取文件列表失败"
}

关键注意点

  • Jenkins Pipeline中,所有涉及文件系统的操作都必须明确指定执行节点,跨节点时绝对不能在主节点直接访问代理节点的本地路径。
  • 优先使用Jenkins提供的内置步骤(如findFiles、dir等)而非原生Groovy的File类,这些步骤已经做了跨平台和跨节点的兼容处理。

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 14:30:56