Interview Prep/Jenkins/Scripted pipeline, Groovy & CPS

Why do some Groovy closures behave unexpectedly in pipelines, for example `list.each { }` or `sort`?

AdvancedConceptScripted pipeline, Groovy & CPS
Answer

CPS has its own implementations of many collection methods, and historically each, collect, sort with closures, and similar methods behaved differently (only the first element processed, or expected to call ... but wound up catching ... warnings). Modern workflow-cps versions support most of them, but sort with a comparator closure and some Comparator-based APIs still misbehave in CPS. Plain for (x in list) loops are always safe; put complex collection manipulation in @NonCPS helpers.

All Jenkins questions Roadmap: Jenkins

More on Scripted pipeline, Groovy & CPS