Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins plugin to fail build based on console output

Is there a plugin to fail a build if a pattern occurs on the console output?

For example:

Build Action - success
Build Action - success
Build Action - error_pattern
Build Action - success

Let's assume that the Jenkins build process does not fail the build on error_pattern, I need to have an external fail trigger of some sort.

EDIT

Looking for a solution to fail the build during, not a post build task.

like image 580
Efekt Avatar asked Dec 10 '12 13:12

Efekt


People also ask

How does Jenkins know when a build fails?

The Build Failure Analyzer Jenkins plugin analyzes the causes of failed builds and presents the causes on the build page. It does this by using a knowledge base of build failure causes maintained from scratch.

How do I run a failed build in Jenkins?

To retry a Jenkins job, you can use the Naginator plugin. Simply install the plugin, and then check the Post-Build action "Retry build after failure" on your project's configuration page. If the build fails, it will be rescheduled to run again after the time you specified.


2 Answers

I've used this answer as the basis of a pipeline script. In the "Build" stage I have two parallel sub-stages - one of them is doing the actual build and outputting to a log, whilst the other sub-stage is grepping the same log. I've negated the exit code (! grep) so that it'll error when the "ERROR:" string is found. Thanks to the failFast setting, this will cause the whole "Build" stage to fail as soon as the string is found. There's an extra grep at the end of the first sub-stage, in case an error was produced right at the end.

I'm tracking the build status with a variable (BUILD_COMPLETE).

pipeline {
    agent {
        label 'master'
    }
    environment {
        BUILD_COMPLETE = false
    }
    stages {
        stage('Build') {
            failFast true
            parallel {
                stage('Building') {
                    steps {
                        sh 'echo foo | tee output.log'
                        sleep 5
                        sh 'echo bar | tee -a output.log'
                        sleep 5
                        sh 'echo ERROR: baz | tee -a output.log'
                        sleep 5
                        sh 'echo qux | tee -a output.log'
                        sleep 5

                        sh '! grep "^ERROR:" output.log'

                        script {
                            BUILD_COMPLETE = true
                        }
                    }
                }
                stage('Monitoring the logs') {
                    steps {
                        script {
                            while (BUILD_COMPLETE != true) {
                                sh '! grep "^ERROR:" output.log'
                                sleep 1
                            }
                        }
                    }
                }
            }
        }
    }
}
like image 163
John Delaney Avatar answered Oct 14 '22 15:10

John Delaney


You should try the Post Build Task plugin. You can search for a pattern and then launch a script.

edit: There is also Text finder plugin, looks better for your problem

like image 32
Stéphane Piette Avatar answered Oct 14 '22 13:10

Stéphane Piette