I’m developing a plugin which runs commands for jobs (mostly kubectl) on the Jenkins node via Launcher. At the moment I have such code:
ByteArrayOutputStream cmdLogStream = new ByteArrayOutputStream();
int status = launcher.launch().stdout(cmdLogStream).cmds(args).join();
if (status != 0) {
printstream.println(cmdLogStream.toString());
}
This works fine and I get the output printed after the command finishes with non-zero error code.
Now I want to improve the output in Jenkins jobs for long running commands. I have a dozen commands which run 5-10 minutes and they output their work to stdout during that time constantly. The problem is Launcher.join() always waits until the command finishes and only then outputs everything in one go.
I cannot wrap my head on where and how to flush stdout as soon as the content is changed so my Jenkins jobs will return that content immediately and inform the end user what’s going on in the job process. Are there any examples of this somewhere? Is this even possible with Launcher?