Rohith
Rohith

Reputation: 356

Jenkins Declarative pipeline: scan logs and fail the job if it has a failure message

I need to scan the Jenkins logs and if it has something like "Failed" in the logs. Jenkins should not continue and mark the job as failed.

Upvotes: 3

Views: 4893

Answers (3)

YaP
YaP

Reputation: 377

With findText from the Text Finder plugin it is easy.

                // stop if failure found
                findText alsoCheckConsoleOutput: true, notBuiltIfFound: true, regexp: "${KEY_ERROR_MSG}"

                if (currentBuild.result == 'NOT_BUILT') {
                    error("${KEY_ERROR_MSG}")
                }

Note that this can be run direct in stages, meaning you don't need to wait for all stages to run this.

Example how to search an error message, mark the step as failed and set the build to unstable (I used this to check connection problems with ftp Publisher):

    // Search console log for error text and set build unstable if found
    findText(textFinders: [
         textFinder(
             regexp: 'ERROR: Exception when publishing', 
             alsoCheckConsoleOutput: true, 
             buildResult: 'UNSTABLE'
         )
    ])  
    script {
        if (currentBuild.result == 'UNSTABLE') {
            catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
                error("FTP publishing build artifacts failed") 
            }
        }
    }

Upvotes: 2

I got it failing with above proposal by having like this in parser config:

# ERRORS
error /make to fail/

And in Jenkins pipeline like this:

node ('') {
    try {
        node {
            echo 'make to fail'
        }
    } finally {
        node {
            step([$class: 'LogParserPublisher', failBuildOnError: true, parsingRulesPath: '/opt/jenkins/log_parsers/log_parser', useProjectRule: false])
        }
    }
}

Anyway have not find way to get build 'UNSTABLE' by using something like this in parser config:

# WARNINGS
warning /make to unstable/

Anyone having ideas how to achieve that?

Upvotes: 0

Nicola Ben
Nicola Ben

Reputation: 11327

The log-parser plugin may be what you need.

It parses the console log generated by the Jenkins build.

You have two useful options:

  • "Mark build Unstable on Warning" option: check to have parsed warnings mark the build 'unstable'.

  • "Mark build Failed on Error" option : check to have parsed errors mark the build 'failed'.

Take a look at: https://wiki.jenkins.io/display/JENKINS/Log+Parser+Plugin

For declarative pipeline, try:

step([$class: 'LogParserPublisher', failBuildOnError: true, parsingRulesPath: '<parser rule file>', useProjectRule: false])

Upvotes: 2

Related Questions