2

I'm coding a library for my Jenkins pipelines. I would like to print different formats for my messages, like [INFO], [WARNING] and so on.

So far I have this:

import org.foo.Output

def call(body) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    def out = new Output()

    node("${config.slaveNodeName}") {
        try {
            stage ('CLONE') {
                out.info("SOME VERY USEFUL INFORMATION")
        ...

And at my org.foo.Output class:

package org.foo

import java.util.logging.Logger


class Output {
    private static final Logger LOGGER = Logger.getLogger(Output.class.getName());

    def info(msg){
        LOGGER.info("${msg}")
        echo "[INFO] ${msg}" <-- gives me an exception described below
    }
}

I can see the [INFO] SOME VERY USEFUL INFORMATION on my Jenkins log, however, I would like to redirect this message to Jenkins output console.

How can I do that?

Exception:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.foog.Output.echo() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [[INFO] SOME VERY USEFUL INFORMATION]
Possible solutions: each(groovy.lang.Closure), info(java.lang.Object), wait(), grep(), any(), find()
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:19)
Valter Silva
  • 15,646
  • 48
  • 123
  • 210

1 Answers1

3

Valter, try this one. It's working in my scripts, I think it would also work in your scripts.

In groovy script, add this:

node("${config.slaveNodeName}") {
        try {
            stage ('CLONE') {
                out.info(this,"SOME VERY USEFUL INFORMATION")

and change the class groovy script like this:

def info(script,msg){
        LOGGER.info("${msg}")
        script.echo "[INFO] ${msg}"
    }
Joshi
  • 465
  • 3
  • 6
  • 20
DavidB.
  • 51
  • 6