All,
I am trying to use workflow-cps-global-lib-plugin to create some custom classes to be used within pipeline Job for some specific use case requirement.
Instantiating the custom class within the pipeline Job. However anything logged within the our custom class is not posted in the Jenkins console.
As a hack, I have tried sending the Listener of the build to the custom class and logging it to it which solves the issue of messages not appearing in the console. However, message get jumbled up and down, as every step within the pipeline is executed in separate context and the log messages sent to from custom class ends up on top of all the messages, which defeats the purpose. see example below.
Is there any other alternatives that we could potentially use, such as creating a workflow step to execute the methods of our custom class, which passes the listener from the correct context.
Appreciate any suggestions, that could help us get over this hurdle as we are running tight timelines to this done. Thanks for your help.
Here is the sample code;
In Pipeline job definition:
def custom_class = new CustomClass(currentBuild.getRawBuild())
echo "starting Master"
custom_class.test_log()
echo "Test Echo"
CustomClass, logger is defined as below.
class CustomClass {
def jenkins_logger
CustomClass(rawbuild) {
this.jenkins_logger = rawbuild.listener.getLogger()
}
def test_log(msg) {
this.jenkins_logger.println(msg)
}
}
Jenkins Console Log
Started by user anonymous
Replayed #14
[Pipeline] node
Running on master in /var/lib/jenkins/jobs/logger_test/workspace@3
Test Message from Custom Class method --> This step is executing after echo step below, but message appears on the Top of the Log.
[Pipeline] {
[Pipeline] echo
starting Master --> This is echo step within the Pipeline Job
[Pipeline] echo
Test Echo -> this is executed after Custom class method.
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS