Julian Veerkamp
Julian Veerkamp

Reputation: 1780

Jenkins Pipeline - Groovy traverse on pwd() results in java.io.FileNotFoundException

I'm trying to traverse through the workspace directory using some groovy code, but the Job fails with java.io.FileNotFoundException: /home/user/JENKINS2_STATE/workspace/job@2 even though the directory exists.

Pipeline:

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
    }
}

The groovy code works locally (without @NonCPS and replacing pwd() with System.getProperty("user.dir")).

Upvotes: 0

Views: 2557

Answers (1)

Jon S
Jon S

Reputation: 16346

The problem is that the groovy script executed on the master, so when you do new File(...) you create a file pointer on the master and not the slave/node/agent. Instead, use findFiles which is available in the Pipeline Utility Steps plugin.

Upvotes: 4

Related Questions