Jenkins自定义插件可以在奴隶日志上掌握吗?

时间:2014-05-20 08:59:53

标签: java jenkins jenkins-plugins

基本上,我有完全相同的问题链接在这里: How can i send data to the logger from FileCallable back to master in a Jenkins plugin?

但我无法让它发挥作用。我编写了自己的自定义Jenkins插件,它必须在奴隶上运行。但是,我想记录主jenkins(或者至少是构建日志)。这是我的代码(为简单起见,有点简单):

public class SanityTestResultsToJUnitXMLBuilder extends Builder implements Serializable  {

  // Some initialization code

  @Override
  public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {   

    // Define what should be run on the slave for this build
    SlaveChannel slaveChannel = new SlaveChannel(sourceDirectories, destinationDirectory, listener.getLogger());
    if (launcher.getChannel().call(slaveChannel).equals("NOK")){
        return false;
    }
    return true;
  }

  private static final class SlaveChannel implements Callable<String, IOException>
  {
    // Again, some initialization code

    public SlaveChannel(String sourceDirectories, String destinationDirectory, PrintStream logger){
        this.sourceDirectories = sourceDirectories;
        this.destinationDirectory = destinationDirectory;
        this.logger = logger;           
    }

    public String call() throws UnknownHostException
    {
        logger.println("Running the SanityTestResultsToJUnitXML plugin on host: " + InetAddress.getLocalHost().getHostName());
        if (startProcess()){
            return "OK";
        } else {
            return "NOK";
        }
    }

    private boolean startProcess(){
      // The code that needs to run client side
    }
  }

现在它在Jenkins中给我以下输出: (这是主记录)

java.io.IOException: Unable to serialize nl.tba.SanityTestResultsToJUnitXML.SanityTestResultsToJUnitXMLBuilder$SlaveChannel@7e2536bb
hudson.remoting.UserRequest.serialize(UserRequest.java:166)
hudson.remoting.UserRequest.<init>(UserRequest.java:62)
hudson.remoting.Channel.call(Channel.java:721)
nl.tba.SanityTestResultsToJUnitXML.SanityTestResultsToJUnitXMLBuilder.perform(SanityTestResultsToJUnitXMLBuilder.java:61)
hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
hudson.model.Build$BuildExecution.build(Build.java:199)
hudson.model.Build$BuildExecution.doRun(Build.java:160)
hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:566)
hudson.model.Run.execute(Run.java:1665)
hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
hudson.model.ResourceController.execute(ResourceController.java:88)
hudson.model.Executor.run(Executor.java:246)
Build step 'Execute SanityTestResultsToJUnitXML task' marked build as failure
Finished: FAILURE

所以基本上,记录器(它是一个PrintStream)是不可序列化的。我如何让我的奴隶登录主詹金斯?

2 个答案:

答案 0 :(得分:2)

侦听器对象是可序列化的。因此,您可以将此对象传递给可调用类构造函数,并在listener.getLogger().println();函数中使用call()

答案 1 :(得分:1)

这应该打印到构建日志:

listener.getLogger().println(your_text);