Jenkins Pipeline - Reading previous stage log

泄露秘密 提交于 2020-01-04 03:53:06

问题


Consider a Jenkins Pipeline with two stages, Stage A then Stage B.

In Stage B, is it possible to parse the logs of Stage A for some particular text?


回答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.




回答2:


Use tee to split the output to both stdout and file. Next parse the file for your text.

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)}"
                    }
                }
            }
        }
    }
}

Pipeline output:

print some Stage_A log content ...
found: Stage_A
Finished: SUCCESS



回答3:


What I finally did was, as suggested, to write to a file (and stdout) using tee.

sh "command | tee <filename>"

Then parse the file as needed, using readFile <filename> to read the file from the workspace.




回答4:


If you want to search for the first occurrance of a pattern, you can also use manager.logContains(regexp) or manager.getLogMatcher(regexp). See my other answer for more details: https://stackoverflow.com/a/39873765/4527766



来源:https://stackoverflow.com/questions/38218987/jenkins-pipeline-reading-previous-stage-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!