解析 Jenkins 控制台输出错误

问题描述 投票:0回答:1

我需要检测 jenkins 管道的控制台输出错误(管道/阶段故障、java 异常等),并制作报告将其发送给我的团队。

我试图找到一个合适的插件,但他们似乎没有一个选项来以编程方式获取错误,然后我尝试在后操作中编写一个 shell 脚本,但它不会解析异常,因为它们只出现在在控制台输出中发布操作。

任何人都可以帮助我解决这个问题吗?

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli
1个回答
0
投票

您可以使用Python模块“python-jenkins”编写Python脚本来实现您想要的任务。 这是 Jenkins python 包装器的完整文档 (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.")
© www.soinside.com 2019 - 2024. All rights reserved.