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] }
}
More on Scripted pipeline, Groovy & CPS
What does a scripted pipeline look like and when would you still write one?IntermediateWhat is CPS and why does Jenkins transform pipeline Groovy?AdvancedWhy does `java.io.NotSerializableException` happen in pipelines?AdvancedWhy do some Groovy closures behave unexpectedly in pipelines, for example `list.each { }` or `sort`?AdvancedWhat is the `script` block in declarative, and what are its limits?BeginnerHow do you share a variable between stages in declarative?Intermediate