Jenkins pipeline capture output of jenkins steps Jenkins pipeline capture output of jenkins steps jenkins jenkins

Jenkins pipeline capture output of jenkins steps


As a workaround we use following LogRecorder class:

class LogRecorder implements Serializable {    def logStart    def logEnd    def currentBuild    def steps    LogRecorder(currentBuild) {        this.currentBuild = currentBuild    }    void start() {        logStart = currentBuild.getRawBuild().getLog(2000)    }    String stop() {        logEnd = currentBuild.getRawBuild().getLog(2000)        getLog()    }    String getLog() {        def logDiff = logEnd - logStart        return logDiff.join('\n')    }}

Depending on your needs you may want to adjust the number of log lines in the calls to getLog().

Possible Usage:

LogRecorder logRecorder = new LogRecorder(currentBuild)logRecorder.start()docker.build(someTag)testResult.stdOut = logRecorder.stop()

Please be aware that it may happen - most probably due to caching issues - that the very last line(s) of the log are sometimes missing. Maybe a sleep would help here. But so far this was not required here.


Here's what I'm using to capture the sha256 of the built docker image

docker.build(someTag)def dockerSha256 = sh(returnStdout: true, script: "docker image inspect $someTag | jq .[0].Id").trim()

I'm using 'jq' to parse the json response

or the groovy way

    def json = sh(returnStdout: true, script: "docker image inspect $someTag").trim()    def obj = new JsonSlurper().parseText(json)    println "raw json: " + obj    println "groovy docker sha256: " + obj[0].Id