I have this jenkins pipeline script (you should be able to just paste it directly into a blank pipeline build)
import hudson.console.LineTransformationOutputStream
import hudson.console.ConsoleLogFilter
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
class MyConsoleLogFilter extends ConsoleLogFilter {
OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
throws IOException, InterruptedException {
return new MyOutputStream(logger, StandardCharsets.UTF_8)
}
}
class MyOutputStream extends LineTransformationOutputStream {
def logger
def charset
MyOutputStream(OutputStream logger, Charset charset) {
this.logger = logger
this.charset = charset
}
void close() throws IOException {
super.close();
logger.close();
}
void eol(byte[] bytes, int len) throws IOException {
String line = charset.decode(ByteBuffer.wrap(bytes, 0, len)).toString();
logger.write("xxx ".getBytes(charset))
logger.write(line.getBytes(charset));
}
}
node {
withContext(new MyConsoleLogFilter()) {
echo 'Hello World'
}
echo 'Hello World'
}
I expect to see the first Hello World prefixed by xxx, because of the wrapping by MyConsoleLogFilter.
But all I see is greyed out console output as follows:
Started by user Admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/mike-learning
[Pipeline] {
[Pipeline] withContext
[Pipeline] {
[Pipeline] echo
[Pipeline] }
[Pipeline] // withContext
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Why is the console output from within the withContext() block being hidden?