Status: Closed (View Workflow)
Resolution: Not A Defect
Jira Pipeline Steps 1.4.4
At the end of my pipeline, I am iterating the changeSets and the commits therein looking for strings that match Jira tickets, in order to update all Jira's with code present in a build. In theory, this could result in multiple Jiras needing to be updated, so this is done in a loop, potentially making multiple calls to jiraAddComment(..). However, the entire step stops (seems to return success) once the first jiraAddComment is completed.
See attached source and output. Following call to jiraAddComment(..), the subsequent echo should fire indicating that execution flow has continued. The first jiraAddComment appears in the log (and the comment appears in Jira). However, no subsequent echos in the log. No subsequent call to jiraAddComment in the log, and no subsequent comments in Jira.
It is as-if execution stops at the first call to jiraAddComment
Experiment #1: Remove @NonCPS - Failed
The stage now fails (red instead of green stage). The log messages indicate it went into jiraAddComment and then hit a "Failed in branch Update Jira". There is a stack-trace later in the log that indicates "java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList"
Experiment #2: (restored @NonCPS) replace invocation of jiraAddComment with echo instead - qualified success
The Stage succeeds. The log messages indicate it goes through the expected pathways - looping and recursing as expected. Message payload looks correct. No errors. Obviously, we are NOT sending anything to Jira with this approach, but it verifies syntax. See output-stubCall.txt for details
This seems to firmly point the finger at the call to jiraAddComment as the culprit
With Experiment #1, you got to deal with the variables those are non serializable in that method to get through that. And from the design you can't invoke pipeline steps from a method with @NonCPS annotation, I don't see any problem with the jiraAddComment step.
You may not call regular (CPS-transformed) methods, or Pipeline steps, from a @NonCPS method, so they are best used for performing some calculations before passing a summary back to the main script.
Experiment #3: Complete replacement of the method with a trivial invocation that invokes jiraAddComment twice - Failure (in same manner as original issue)
See output-truncatedCall.txt. The stage succeeds (green and not red). However, logs indicate that we only invoked the first jiraAddComment and no further steps were performed (like the original bug). Inspection in Jira confirms that only the FIRST jiraAddComment was received.
But the code is still in @NonCPS method with Experiment #3, try invoking these steps outside that method.
@NonCPS functions should not use Pipeline steps internally, however you can store the result of a Pipeline step to a variable and use it that as the input to a @NonCPS function. Gotcha: It’s not guaranteed that use of a step will generate an error (there is an open RFE to implement that), but you should not rely on that behavior. You may see improper handling of exceptions, in particular.
Experiment #4: calling jiraAddComment twice directly from steps(), i.e. no method call, definitely works. Just verified that
yeah, as I mentioned in the above comments, pipeline steps has be outside the @NonCPS function.
I'm inferring that if I use the @NonCPS method to build an array of Jiras to update (ID + Comment), pass the array back to the stage, iterate and call jiraAddComment for each element in the array (from the stage), it would work. I think?
My groovy skills are weak. Built an array in the method, but cannot figure out how to get it back to the stage
OK resolved it - many thanks. Indeed, you have to build an array of Jiras to update in a method, then do the updating back in the stage (inside a script marker).
I added my final code to output-final.txt in the hopes maybe it will benefit the next person.
nrayapati Thank you for the help!
Issue can be closed
I doubt it is either @NonCPS annotation or the msg.findAll, cause not all the steps can be invoked in @NonCPS method and also not all the groovy syntax supported in Jenkins pipeline especially those loops and methods on collections.
Can you try: