解析Jenkins控制台输出错误

nnsrf1az  于 12个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(156)

我需要检测jenkins管道的控制台输出错误(管道/阶段故障,java异常等),并制作一份报告发送给我的团队。
我试图找到一个合适的插件,但他们似乎没有一个选项来自动获取错误,然后我试图在post操作中编写一个shell脚本,但它不会解析异常,因为它们只出现在控制台输出中的post操作之后。
有人能帮我解决这个问题吗?

x4shl7ld

x4shl7ld1#

您可以使用Python模块'python-jenkins'编写Python脚本来实现您想要的任务。这里是Jenkins python Package 器(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.")

字符串

相关问题