Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-53151

withContext not working as expected in combination with consoleLogFilter

XMLWordPrintable

      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

            Unassigned Unassigned
            roel0 roel postelmans
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: