Interview Prep/Jenkins/Scripted pipeline, Groovy & CPS

What does `@NonCPS` do and what are its rules?

AdvancedConceptScripted pipeline, Groovy & CPS
Answer

It marks a method to run as ordinary compiled Groovy, outside CPS. It is faster and can use non-serializable objects internally. Rules:

  • It must not call pipeline steps (sh, echo, readFile); behaviour is undefined (usually the step runs once and the method returns oddly).
  • It cannot be resumed mid-method after a restart.
  • Its return value goes back into CPS code, so it must be serializable.
@NonCPS
List<String> extractVersions(String text) {
  (text =~ /v(\d+\.\d+\.\d+)/).collect { it[1] }
}
All Jenkins questions Roadmap: Jenkins

More on Scripted pipeline, Groovy & CPS