Problem looping through the result of findFiles.
The result from this method returns a 
org.jenkinsci.plugins.pipeline.utility.steps.fs.FileWrapper

node {
    stage("Publish") {        
        def files = findFiles(glob: '**/*.rpm')

        for (def file : files) {
            sh "echo Hello World! ${file.path}"
        }
}
Fails with java.io.NotSerializableException: java.util.AbstractList$Itr

Trying to use @NonCPS
node {
    stage("Publish") {
        def files = findFiles(glob: '**/*.rpm')
        loopFiles(files)
    }
}

@NonCPS
void loopFiles(files) {
    for (def file : files) { 
        sh "echo Hello World! ${file.path}"
    }
}
It breaks the loop after first item in list. If 3 files found, then there 
is only one output.
However, if I remove the shell script and just use println it loops through 
all 3 files found.


Works if I loop the files first and put the file path in a new list:
node {
    stage("Publish") {
        def files = findFiles(glob: '**/*.rpm')
        def packages = []
        for (def file : files) {
            packages.add(file.path)
        }

        for (def packagePath : packages) {
            sh "echo Hello World! ${packagePath}"
        }
}

I reckon that FileWrapper is not serializable, hence the problem, but why 
doesn't the @NonCPS work on all items?

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/4efcd7bb-a706-42b0-a7b8-88fdd1434783%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to