Parsing Jenkins console output errors

96 views Asked by At

I need to detect the errors of console output(pipeline/stage failure, java exceptions, etc...) of a jenkins pipeline and make a report to send it to my team.

I tried to find a suitable plugin but they don't seem to have an option to programatically get the errors, then I tried to write a shell script in the post actions but it wouldn't parse the exceptions because they only appear after the post actions in the console output.

Can anyone help me on how to approach to this problem?

1

There are 1 answers

0
Anandhi On

You can write a Python script using the Python module, 'python-jenkins' to achieve your desired task. Here is the complete documentation for the Jenkins python wrapper (https://pypi.org/project/python-jenkins/)

import jenkins #importing jenkins python module

# Replace these values with your Jenkins server information
jenkins_url = 'http://your-jenkins-server-url'
username = 'your-username'
password = 'your-password'

# Connecting to Jenkins server
server = jenkins.Jenkins(jenkins_url, username=username, password=password)

# Specify Jenkins job name and build number
job_name = 'your-job-name'
build_number = 'your-build-number'  # This can be 'lastBuild' for the latest build

# Get console output (log)
console_output = server.get_build_console_output(job_name, build_number)

# Get build information
build_info = server.get_build_info(job_name, build_number)

# Check for build result (success or failure)
if build_info['result'] == 'FAILURE':
    # Retrieve errors from the console output
    errors = [line for line in console_output.split('\n') if line.startswith('[ERROR]')] # you can modify the keyword based on your requirement

    # Print errors
    print("\nErrors:")
    for error in errors:
        print(error) #you can print the errors or you can send the output to an excel or text document to prepare reports
else:
    print("\nBuild was successful.")