Iterate through filesystem from Jenkinsfile using Groovy

Viewed 1089

I'm trying to iterate through the whole directory structure of a git repository from a Jenkins pipeline using groovy. But somehow my loop always stops after the first iteration.

My pipeline sets up multiple parallel stages automatically. Each of these stages should loop through their part of the filesystem (each stage clones a git repo and should loop through the files)

// retrieve projects list in an earlier stage

def branchedStages = [:]
    projects.each { val ->

        // set some vars

        branchedStages["${project}"] = {
            stage("build::${project}") {

                // some stuff that is working just fine

                dir("${reposDir}/${projectTitle}/${projectName}") {
                    sh "pwd"
                    sh "ls -alF"

                    echo "[INFO] Iterate project"
                    def dir = new File("${reposDir}/${projectTitle}/${projectName}")
                    dir.traverse(type: ANY) {f ->
                        println f.name
                        println f.absolutePath

                        if(f.name.endsWith('.sh')) {
                            println "..."
                            // todo: do the stuff I actually wanna do
                        }
                    }
                }
            }
        }

        parallel branchedStages
    }
}

This is the snippet from my pipelins script. The ls -alF confirms, that there are more than only one file inside the directory. Still the groovy code only shows one entry and stops afterwards.

The var final String reposDir="/tmp/repos" is set to an absolute path so there is no problem with being at the wrong position inside my directory tree.

This is the output from my pipeline (I removed some of the steps that are working correctly for better reading).

enter image description here

I don't know why my groovy snippet stops after the first file. My Groovy Version is 2.4.12.

0 Answers
Related