Jenkins Pipeline - 读取前一阶段日志
Jenkins Pipeline - Reading previous stage log
考虑具有两个阶段的 Jenkins 管道,阶段 A 然后是 阶段 B。
在阶段 B 中,是否可以针对某些特定文本解析阶段 A 的日志?
我最后做的是,按照建议,使用 tee 写入文件(和标准输出)。
sh "command | tee <filename>"
然后根据需要解析文件,使用readFile <filename>
从工作区读取文件。
自 7 月 28 日以来有更新!
如 , as of version 2.4 of Pipeline: Nodes and Processes 中所述,您可以使用:
def out = sh script: 'command', returnStdout: true
至少比输出到一个文件然后再读取文件简单干净多了。
如果要搜索第一次出现的模式,也可以使用 manager.logContains(regexp)
或 manager.getLogMatcher(regexp)
。有关详细信息,请参阅我的其他答案:
使用tee
将输出拆分为标准输出和文件。接下来为您的文本解析文件。
STAGE_A_LOG_FILE = 'stage_a.log'
pipeline {
agent any
stages {
stage('Stage A') {
steps {
script {
// tee log into file
tee(STAGE_A_LOG_FILE) {
echo 'print some Stage_A log content ...'
}
}
}
}
stage('Stage B') {
steps {
script {
// search log file for 'Stage_A'
regex = java.util.regex.Pattern.compile('some (Stage_A) log')
matcher = regex.matcher(readFile(STAGE_A_LOG_FILE))
if (matcher.find()) {
echo "found: ${matcher.group(1)}"
}
}
}
}
}
}
管道输出:
print some Stage_A log content ...
found: Stage_A
Finished: SUCCESS
考虑具有两个阶段的 Jenkins 管道,阶段 A 然后是 阶段 B。
在阶段 B 中,是否可以针对某些特定文本解析阶段 A 的日志?
我最后做的是,按照建议,使用 tee 写入文件(和标准输出)。
sh "command | tee <filename>"
然后根据需要解析文件,使用readFile <filename>
从工作区读取文件。
自 7 月 28 日以来有更新!
如
def out = sh script: 'command', returnStdout: true
至少比输出到一个文件然后再读取文件简单干净多了。
如果要搜索第一次出现的模式,也可以使用 manager.logContains(regexp)
或 manager.getLogMatcher(regexp)
。有关详细信息,请参阅我的其他答案:
使用tee
将输出拆分为标准输出和文件。接下来为您的文本解析文件。
STAGE_A_LOG_FILE = 'stage_a.log'
pipeline {
agent any
stages {
stage('Stage A') {
steps {
script {
// tee log into file
tee(STAGE_A_LOG_FILE) {
echo 'print some Stage_A log content ...'
}
}
}
}
stage('Stage B') {
steps {
script {
// search log file for 'Stage_A'
regex = java.util.regex.Pattern.compile('some (Stage_A) log')
matcher = regex.matcher(readFile(STAGE_A_LOG_FILE))
if (matcher.find()) {
echo "found: ${matcher.group(1)}"
}
}
}
}
}
}
管道输出:
print some Stage_A log content ...
found: Stage_A
Finished: SUCCESS