如何在Jenkins控制台输出上写?

时间:2017-10-24 13:41:52

标签: jenkins groovy jenkins-plugins jenkins-pipeline

我为Jenkins管道编写了一个库。我想为我的消息打印不同的格式,例如[INFO], [WARNING]等等。

到目前为止,我有这个:

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")
        ...

在我的org.foo.Output班级:

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
    }
}

我可以在Jenkins日志上看到[INFO] SOME VERY USEFUL INFORMATION,但是,我想将此消息重定向到Jenkins输出控制台。

我该怎么做?

例外:

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)

1 个答案:

答案 0 :(得分:2)

Valter,试试这个。它在我的脚本中工作,我认为它也适用于你的脚本。

在groovy脚本中,添加:

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

并更改类groovy脚本,如下所示:

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