Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Jenkins custom plugin on slave log to master?

Basicaly, I have exact the same question as linked here: How can i send data to the logger from FileCallable back to master in a Jenkins plugin?

but I can't get it to work. I have written my own custom Jenkins plugin and it has to run on a slave. I want to log, however, to the master jenkins (or at least to the build log). Here's my code (cut a bit for simplicity):

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

Right now it gives me the following output in Jenkins: (And this is the master logging)

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

So basicaly, the logger (which is a PrintStream) is not serializable. How do I still get my slave to log to the main Jenkins?

like image 955
Rens Groenveld Avatar asked May 20 '14 08:05

Rens Groenveld


1 Answers

Listener object is serializable. So, you can pass this object to the callable class constructor, and use listener.getLogger().println(); in the call() function.

like image 106
Shankar Ramasamy Avatar answered Oct 10 '22 09:10

Shankar Ramasamy