当前位置: 首页 > 工具软件 > dict_build > 使用案例 >

jenkins获取build_info

公良照
2023-12-01

import jenkins
import  time
from urllib.parse  import  urlparse

def LoginJenkins(url, username=None, password=None):
    return jenkins.Jenkins(url,username,password)



class BuildInfo(object):
    # Build_Info初始化,默认参数是: jenkins实例对象,工程名,构建号。
    def __init__(self,InstanceJenkins,job_name,buildnumber):
        self.build_info = InstanceJenkins.get_build_info(job_name, buildnumber)
        self.initializa()

    # @classmethod
    # def init_use_url(cls,url,username=None, password=None):
    #     print(urlparse(url))
    #
    #     return  cls()




    def initializa(self):
        self.building = self.build_info.get('building', '')
        self.result = self.build_info.get('result', "")  # 有几种状态 暂时未知   ABORTED  SUCCESS  FAILURE null,已经"building": true, 正在构建
        self.build_start_time = int(self.build_info.get('timestamp', 0) / 1000)
        self.build_use_time = int(self.build_info.get('duration', 0) / 1000)
        self.build_end_time = self.build_start_time + self.build_use_time
        self.build_url =  self.build_info.get('url','')


    # def keys(self):
    #     return ('building', 'result','build_start_time','build_use_time',
    #             'build_end_time','build_url')

    def __getitem__(self, item):
        return getattr(self, item)

    # 没加到 初始化中去。
    def get_current_build_parmars(self):
        self.parmas_dict = {}
        if 'actions' in self.build_info.keys():
            build_actions_list = self.build_info.get('actions', [])
            for item_dict in build_actions_list:
                if 'parameters' in item_dict.keys():
                    parameters_list = item_dict.get('parameters', [])
                    for parmas_dict_child_dict in parameters_list:
                        self.parmas_dict[parmas_dict_child_dict.get('name', '')] = parmas_dict_child_dict.get('value', '')


    def get_current_build_one_parmar(self,parmar):
        self.parmas_dict = {}
        if 'actions' in self.build_info.keys():
            build_actions_list = self.build_info.get('actions', [])
            for item_dict in build_actions_list:
                if 'parameters' in item_dict.keys():
                    parameters_list = item_dict.get('parameters', [])
                    for parmas_dict_child_dict in parameters_list:
                        if parmas_dict_child_dict.get('name', '') == parmar:
                            return parmas_dict_child_dict.get('value', '')



if __name__ == '__main__' :

    InstanceJenkins = LoginJenkins("http://10.0.0.40:8080/","admin","admin")
    build = BuildInfo(InstanceJenkins,'U',22)
    ret = build.get_current_build_one_parmar('tag')
    print(build.building)












 类似资料: