If I have a Pipeline script method in Pipeline script (Jenkinsfile
), my Global Pipeline Library's vars/
or in a src/
class, how can obtain the OutputStream
for the console log? I want to write directly to the console log.
I know I can echo
or println
, but for this purpose I need to write without the extra output that yields. I also need to be able to pass the OutputStream
to something else.
I know I can call TaskListener.getLogger()
if I can get the TaskListener
(really hudson.util.StreamTaskListener
) instance, but how?
I tried:
I've looked into
manager.listener.logger
(from the groovy postbuild plugin) and in the early-build context I'm calling from it doesn't yield an OutputStream that writes to the job's Console Log.echo "listener is a ${manager.listener} - ${manager.listener.getClass().getName()} from ${manager} and has a ${manager.listener.logger} of class ${manager.listener.logger.getClass().getName()}"
prints
listener is a hudson.util.LogTaskListener@420c55c4 - hudson.util.LogTaskListener from org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder$BadgeManager@58ac0c55 and has a java.io.PrintStream@715b9f99 of class java.io.PrintStream
I know you can get it from a
StepContext
viacontext.get(TaskListener.class)
but I'm not in aStep
, I'm in aCpsScript
(i.e.WorkflowScript
i.e.Jenkinsfile
).- Finding it from a
CpsFlowExecution
obtained from theDSL
instance registered as thesteps
script-property, but I couldn't work out how to discover theTaskListener
that's passed to it when it's created
How is it this hard? What am I missing? There's so much indirect magic I find it incredibly hard to navigate the system.
BTW, I'm aware direct access is blocked by Script Security, but I can create @Whitelisted
methods, and anything in a global library's vars/
is always whitelisted anyway.