Jenkins管道脚本中的withContext()隐藏控制台输出 [英] withContext() in Jenkins pipeline script is hiding console output
问题描述
我有这个詹金斯管道脚本(您应该可以将其直接粘贴到空白管道版本中)
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'
}
由于MyConsoleLogFilter
的换行,我希望看到第一个Hello World
以xxx
为前缀.
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
为什么withContext()
块中的控制台输出被隐藏了?
Why is the console output from within the withContext()
block being hidden?
推荐答案
一位同事(Hibri Marzook)为我解决了这个问题.他将我指向 https://issues.jenkins-ci.org/browse/JENKINS- 53151 ,这促使我这样做:
A colleague (Hibri Marzook) solved this for me. He pointed me towards https://issues.jenkins-ci.org/browse/JENKINS-53151 , which prompted me to do this:
import hudson.console.LineTransformationOutputStream
import hudson.console.ConsoleLogFilter
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
class MyConsoleLogFilter extends ConsoleLogFilter {
@NonCPS
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
}
@NonCPS
void close() throws IOException {
super.close();
logger.close();
}
@NonCPS
void eol(byte[] bytes, int len) throws IOException {
def line = charset.decode(java.nio.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'
}
现在可以使用了.
这篇关于Jenkins管道脚本中的withContext()隐藏控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!