user3278960
user3278960

Reputation: 11

Jenkins .eachDir() iterate only once

I am trying to do a simple housekeeping pipeline to delete old workspace dirs within Jenkins.

node {
   stage 'Housekeeping stage'
   echo "Deleting all old cell directories, older then ${env.MAXIMUM_CELL_LIVE} days"
   new File("${env.phaser_dir}\\workspace\\").eachDir() { dir ->  
       long diff = new Date().getTime() - dir.lastModified()
       if (diff > env.MAXIMUM_CELL_LIVE.toInteger() * 24 * 60 * 60 * 1000) {
            dir.deleteDir()
        }
    }  
}

The result is that it iterates only once each time, deleting just one directory.

I have latest version of Pipeline at 2.2. I've also googled there used to be problems like this with .each iterator, but that supposed to be fixed?

Many Thanks Michal

Upvotes: 1

Views: 3485

Answers (1)

Radim
Radim

Reputation: 11

This is a known issue with Jenkins pipelines (former workflows) and it's filed in JIRA as JENKINS-26481.

Note that there's a lot happening behind the scenes in the Jenkins workflow; after every line Jenkins saves the state of the workflow (position in loops, local variables etc.) to be able to survive failure and resume processing. That's why fixing this problem within Jenkins is not trivial.

There's an easy workaround for you though - just move the logic into a separate function with @NonCPS annotation.

More information is available in the plugin documentation.

Upvotes: 1

Related Questions