Search code examples
jenkinsjenkins-pipelinepipelinedeclarative

how to fail the jenkins build if any test cases are failed using findText plugin


I have a stage in Jenkins as follows, How do I mark the build to fail or unstable if there is a test case failure? I generated the script pipeline for textfinder plugin but it is not working. "findText alsoCheckConsoleOutput: true, regexp: 'There are test failures.', unstableIfFound: true" not sure where to place the textFinder regex.

pipeline {

agent none
tools {
    maven 'maven_3_6_0'
    
}

options {
    timestamps ()
    buildDiscarder(logRotator(numToKeepStr:'5'))
}

environment {
    JAVA_HOME = "/Users/jenkins/jdk-11.0.2.jdk/Contents/Home/"
    imageTag = ""
}

parameters {
    choice(name: 'buildEnv', choices: ['dev', 'test', 'preprod', 'production', 'prodg'], description: 'Environment for Image build')
    choice(name: 'ENVIRONMENT', choices: ['dev', 'test', 'preprod', 'production', 'prodg'], description: 'Environment for Deploy')
}

stages {
    stage("Tests") {
        agent { label "xxxx_Slave"}
            steps {
                checkout([$class: 'GitSCM', branches: [[name: 'yyyyyyyyyyz']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'zzzzzzzzzzz', url: 'abcdefgh.git']]])
                sh'''
                cd dashboard
                mvn -f pom.xml surefire-report:report -X -Dsurefire.suiteXmlFiles=src/test/resources/smoke_test.xml site -DgenerateReports=false
                '''
            }
            
    }
}

}


Solution

  • All I did to make this request possible is as below:

    added a post block of code below the steps block code.

    post { Success { findText alsoCheckConsoleOutput: true, refexp: 'There are test failures.', unstableIfFound: true } }