Jenkins Pipeline Script for Automated Failure Detection in Console Output

Introduction:
Jenkins pipelines play a crucial role in automating software delivery. One common requirement is to detect failures within the pipeline and take appropriate actions. In this article, we’ll explore a generic Jenkins pipeline script for failure detection, allowing you to customize it for your specific needs.

Script Overview:
The following Jenkins pipeline script is designed to check for failure messages in the console output and take action accordingly. This generic script can be easily adapted for various scenarios, making it a versatile solution for failure detection.

pipeline {
    // Your pipeline stages go here

    post {
        always {
            script {
                
                def consoleOutput = currentBuild.rawBuild.getLog(1000) // Adjust the number of lines to capture if needed

            // Convert consoleOutput to a string
            def consoleOutputString = consoleOutput.join('\n')

            // Print the entire console output
            echo "Full Console Output:"
            consoleOutputString.eachLine { line ->
                echo line
                if (line.contains('  YOUR_FAILURE_CONDITION_HERE')) {
                    echo "Found the problematic line: ${line}"
                }
            }

            // Store consoleOutputString as an environment variable
            env.CONSOLE_OUTPUT_STRING = consoleOutputString

            // Debug statement to print the value of env.CONSOLE_OUTPUT_STRING
            echo "DEBUG: env.CONSOLE_OUTPUT_STRING = ${env.CONSOLE_OUTPUT_STRING}"

            // Check for failure message using the environment variable
            if (env.CONSOLE_OUTPUT_STRING && env.CONSOLE_OUTPUT_STRING.contains('YOUR_FAILURE_CONDITION_HERE')) {
                echo 'Test failed!'
                currentBuild.result = 'FAILURE'
            } else {
                echo 'Test succeeded!'
            }

            // Additional post-build actions go here
        }
    }
}

Customization Steps:

  1. Insert Your Stages: Add your specific pipeline stages within the pipeline block.
  2. Define Failure Condition: Customize the contains method in the if condition to match your failure criteria. This can be a specific text message, a keyword, or any pattern indicating failure.
  3. Adapt Post-Build Actions: Modify the post-build actions as per your requirements, such as sending notifications, cleaning up resources, or triggering additional processes.

Usage:

  1. Copy the provided script and paste it into your Jenkinsfile.
  2. Customize the failure condition based on your project’s specific failure messages.
  3. Integrate additional post-build actions according to your workflow.

Conclusion:
By using this generic Jenkins pipeline script, you can easily implement failure detection tailored to your project’s needs. This approach enhances automation, ensuring timely responses to pipeline issues and streamlining your continuous integration and delivery processes.