Jenkins管道 - 在pwd()上进行Groovy遍历导致java.io.FileNotFoundException

时间:2017-04-21 11:56:42

标签: jenkins groovy continuous-integration jenkins-pipeline

我尝试使用一些常规代码遍历工作区目录,但即使目录存在,作业也会以java.io.FileNotFoundException: /home/user/JENKINS2_STATE/workspace/job@2失败。

管道:

import groovy.io.FileType
import com.cloudbees.groovy.cps.NonCPS

@NonCPS
def traverseHelper() {
    new File(pwd()).traverse(type: FileType.FILES) {
        println it.path
    }
}

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                parallel(
                    "Linux": {
                        node(label: 'lnx') {
                            script {
                                //checking out from git here
                                traverseHelper()
                            }
                        }
                    },
                    "Windows": {
                        node(label: 'win') {
                            script {
                                //do Windows stuff here
                            }
                        }
                    }
                )
            }
        }
        //other stages here
    }
}

groovy代码在本地工作(没有@NonCPS并将pwd()替换为System.getProperty("user.dir"))。

1 个答案:

答案 0 :(得分:2)

问题是在主服务器上执行了groovy脚本,所以当你执行new File(...)时,你在主服务器而不是从服务器/节点/代理上创建了一个文件指针。相反,请使用findFiles中提供的Pipeline Utility Steps plugin