詹金斯管道 - 阅读前一阶段日志

时间:2016-07-06 08:00:30

标签: jenkins jenkins-pipeline

考虑一个Jenkins管道,分为两个阶段,阶段A 然后阶段B

在阶段B中,是否可以针对某些特定文本解析阶段A的日志?

4 个答案:

答案 0 :(得分:1)

There's been an update since July 28th !

As mentionned in this answer, as of version 2.4 of Pipeline: Nodes and Processes you can use:

def out = sh script: 'command', returnStdout: true

At least it's much more simple and clean than outputting to a file and reading the file afterwards.

答案 1 :(得分:1)

使用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

答案 2 :(得分:0)

根据建议,我最终做的是使用tee写入文件(和stdout)。

sh "command | tee <filename>"

然后根据需要解析文件,使用readFile <filename>从工作区读取文件。

答案 3 :(得分:0)

如果您要搜索模式的首次出现,您还可以使用manager.logContains(regexp)manager.getLogMatcher(regexp)。有关详细信息,请参阅我的其他答案:https://stackoverflow.com/a/39873765/4527766