Chris
Chris

Reputation: 119

Jenkins - Groovy pipeline - Using FilePath gives Could not find matching constructor

Building on this suggestion: Using FilePath to access workspace on slave in Jenkins pipeline I am trying to get the files that are dropped into the workspace by git, and iterate through those files using eachFileRecurse, by passing in the adjusted folder. However, when calling into FilePath, I get errors.

    import groovy.io.FileType
    import java.io.File
    import java.lang.Object
    import hudson.FilePath
    import jenkins.model.Jenkins

    def createFilePath(path) {
    return new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
    }

@NonCPS // has to be NonCPS or the build breaks on the call to .each
def getFiles(dirLoc) {
    def dir = new File (dirLoc)
    def list = []
    dir.eachFileRecurse (FileType.FILES)
            {file -> if (file.name.endsWith('.txt')) {list << file}}
    return list
}

I get this error

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(hudson.FilePath)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1737)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1537)

I cannot figure out what I am missing here to make this work. Thanks in advance!

Upvotes: 1

Views: 3666

Answers (1)

Gon
Gon

Reputation: 182

There is no Constructor for java.io.file with hudson.FilePath as a parameter.

Refer to java doc at following link: https://docs.oracle.com/javase/7/docs/api/java/io/File.html

Upvotes: 1

Related Questions