[英]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? 基本上,我有完全相同的问题链接在这里: 如何在Jenkins插件中将数据从FileCallable发送回主机?
but I can't get it to work. 但我无法让它发挥作用。 I have written my own custom Jenkins plugin and it has to run on a slave. 我编写了自己的自定义Jenkins插件,它必须在奴隶上运行。 I want to log, however, to the master jenkins (or at least to the build log). 但是,我想记录主jenkins(或者至少是构建日志)。 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) 现在它在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
So basicaly, the logger (which is a PrintStream) is not serializable. 所以基本上,记录器(它是一个PrintStream)是不可序列化的。 How do I still get my slave to log to the main Jenkins? 我如何让我的奴隶登录主詹金斯?
Listener object is serializable. 监听器对象是可序列化的。 So, you can pass this object to the callable class constructor, and use listener.getLogger().println();
因此,您可以将此对象传递给可调用类构造函数,并使用listener.getLogger().println();
in the call()
function. 在call()
函数中。
这应该打印到构建日志:
listener.getLogger().println(your_text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.