Jenkinsapi 调用

一、安装

pip install jenkinsapi

二、使用

from jenkinsapi.jenkins import Jenkins

job_name  = '服务名称'
# 定义连接对象
server = Jenkins('url','username','password')

if server.has_job(job_name):
    my_job = server.get_job(job_name)
    if not my_job.is_queued_or_running():
        try:
            last_build = my_job.get_last_buildnumber()
        except Exception as e:
            last_build = 0
        build_num += 1
        # 开始打包
        server.build_job(job_name)
        
        # 循环判断Jenkins是否打包完成
        while True:
            if not my_job.is_queued_or_running():
                # 获取最新一次打包信息
                count_build = my_job.get_build(build_num)
                # 获取打包开始时间
                start_time = count_build.get_timestamp()
                # 获取打包日志
                console_out = count_build.get_console()
                # 获取状态
                status = count_build.get_status()
    else:
        print('Jenkins is running')
else:
    print('没有该服务')

你可能感兴趣的:(Jenkinsapi 调用)