jenkinsci / jenkinsci/workflow-basic-steps-plugin
[JENKINS-53151] withContext not working as expected in combination with consoleLogFilter
- Dominant language
- Java
- Stars
- 73
- Forks
- 129
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to make a custom ConsoleLogFilter in my pipeline script, however for some reason it breaks the echo functionality.
Here's my code:
final class TestStream extends OutputStream {
private final OutputStream delegate;
TestStream(OutputStream delegate) {
this.delegate = delegate
}
public void write(int b) throws IOException {
delegate.write(b);
}
public void write(byte[] b) throws IOException {
delegate.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
delegate.write(b, off, len);
}
public void flush() throws IOException {
delegate.flush();
}
public void close() throws IOException {
delegate.close();
}
}
class TestFilter extends ConsoleLogFilter {
@Override
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
throws IOException, InterruptedException {
if (logger == null) {
return null;
}
return new TestStream(logger)
}
}
ConsoleLogFilter subsequent = new TestFilter();
ConsoleLogFilter original = getContext(ConsoleLogFilter.class);
println original //prints null
timestamps {
original = getContext(ConsoleLogFilter.class);
println original //prints TimestampNotesConsoleLogFilter object
echo "Out of Context" //Visible
withContext(BodyInvoker.mergeConsoleLogFilters(original, subsequent)) {
echo "In context " //Not visible
}
withContext(new TestFilter()) {
echo "In context " //Not visible
}
}
Here is the output:
Started by user jenkins
Running in Durability level: PERFORMANCE_OPTIMIZED
[Pipeline] getContext
[Pipeline] echo
null
[Pipeline] timestamps
[Pipeline] {
[Pipeline] getContext
[Pipeline] echo
07:22:58 hudson.plugins.timestamper.pipeline.TimestamperStep$TimestampNotesConsoleLogFilter@1d678d7a
[Pipeline] echo
07:22:58 Out of Context
[Pipeline] withContext
[Pipeline] {
[Pipeline] echo
[Pipeline] }
[Pipeline] // withContext
[Pipeline] withContext
[Pipeline] {
[Pipeline] echo
[Pipeline] }
[Pipeline] // withContext
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] End of Pipeline
Finished: SUCCESS
The 'in context' print is never written to the outputstream
---
Originally reported by roel0, imported from: withContext not working as expected in combination with consoleLogFilter
Raw content of original issue
I'm trying to make a custom ConsoleLogFilter in my pipeline script, however for some reason it breaks the echo functionality.
Here's my code:
final class TestStream extends OutputStream {
private final OutputStream delegate;
TestStream(OutputStream delegate) {
this.delegate = delegate
}
public void write(int b) throws IOException {
delegate.write(b);
}
public void write(byte[] b) throws IOException {
delegate.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
delegate.write(b, off, len);
}
public void flush() throws IOException {
delegate.flush();
}
public void close() throws IOException {
delegate.close();
}
}
class TestFilter extends ConsoleLogFilter {
@Override
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
throws IOException, InterruptedException {
if (logger == null) {
return null;
}
return new TestStream(logger)
}
}
ConsoleLogFilter subsequent = new TestFilter();
ConsoleLogFilter original = getContext(ConsoleLogFilter.class);
println original //prints null
timestamps {
original = getContext(ConsoleLogFilter.class);
println original //prints TimestampNotesConsoleLogFilter object
echo "Out of Context" //Visible
withContext(BodyInvoker.mergeConsoleLogFilters(original, subsequent)) {
echo "In context " //Not visible
}
withContext(new TestFilter()) {
echo "In context " //Not visible
}
}Here is the output:
Started by user jenkins
Running in Durability level: PERFORMANCE_OPTIMIZED
[Pipeline] getContext
[Pipeline] echo
null
[Pipeline] timestamps
[Pipeline] {
[Pipeline] getContext
[Pipeline] echo
07:22:58 hudson.plugins.timestamper.pipeline.TimestamperStep$TimestampNotesConsoleLogFilter@1d678d7a
[Pipeline] echo
07:22:58 Out of Context
[Pipeline] withContext
[Pipeline] {
[Pipeline] echo
[Pipeline] }
[Pipeline] // withContext
[Pipeline] withContext
[Pipeline] {
[Pipeline] echo
[Pipeline] }
[Pipeline] // withContext
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] End of Pipeline
Finished: SUCCESS
The 'in context' print is never written to the outputstream
Contributor guide
Assessment
This issue has not been assessed yet.