Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@NonCPS stops after first build step

In a @NonCPS annotated function, only code up to the very first jenkins build step is executed. Does anyone have the same problem? Am I missing something? I am using Jenkins LTS... just sayin' (2.73.2).

This is my code:

@NonCPS
def hello() {
    println 'Output "hello":'
    sh 'echo Hello'
    println 'Output "World":'
    sh 'echo World'
}

node {
    stage('Test') {
        hello()
    }
}

I would expect this code to run properly, but the output is the following:

[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Sandbox/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Output "hello":
[Pipeline] sh
[pipeline-test] Running shell script
+ echo Hello
Hello
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
like image 779
Jazzschmidt Avatar asked Oct 11 '25 07:10

Jazzschmidt


1 Answers

You cannot run build steps inside @NonCPS methods. Pipeline scripts are considered "serializable", allowing them to be durable across system failures etc. Only a subset of the capabilities of groovy used by pipeline scripts is serializable - for anything that is not, you use @NonCPS to execute it.

Essentially, your @NonCPS method needs to do its business and return data back to the "safe", serialized execution stack.

In your particular example code I see no reason why hello() has to be @NonCPS at all - I can only assume your real function is doing something more complex.

(Edit) Having just looked at your question history and the original script; I don't know if this is still the case with the latest versions but when I was writing our scripts ~6 months ago, each { thing -> iteration was not serializable.

like image 108
badgerr Avatar answered Oct 14 '25 07:10

badgerr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!