jenkins的api调用python篇(三)秀代码

  • Post author:
  • Post category:python


秀代码

#!/usr/bin/env python
#coding=utf-8
import jenkins
import time
import requests
import re
import threading


class Python_Jenkins_Api_Test(object):
    def __init__(self, *args):
        self.jenkins_server_url = 'http://jenkins.jamesding.top/' #定义远程的jenkins master server的url,以及port
        self.user_id = 'dingyunlong' #定义用户的User Id 和 API Token,获取方式同上文
        self.api_token = '******'
        # 实例化jenkins对象,连接远程的jenkins master server
        self.server = jenkins.Jenkins(self.jenkins_server_url, username=self.user_id, password=self.api_token)
        self.user = 'dingyunlong'
        self.passwd = 'root123'


    def work_with_jenkins_job(self):
        self.server.build_job('Sonar_Logger') #执行job
        #time.sleep(80) #暂停80秒,等待job执行结束
        last_build_number = self.server.get_job_info('Sonar_Logger')['lastCompletedBuild']['number'] #构建完成的job
        build_info = self.server.get_build_info('Sonar_Logger', last_build_number)['url']
        global  console_info
        console_info = self.server.get_build_console_output('Sonar_Logger', last_build_number)
        global url_console #声明全局变量放在之前
        url_console = build_info+'console'
        return

    def get_sonar_url(self):
        print url_console
        #r1 = requests.get(url)
        #print r1
        return

    def get_sonar_url_true(self):
        r1 = r'http://10.127.96.124:9000/.*'
        try:
            r2 = re.findall(r1, console_info)
            print r2[0]
        except:
            print 'code error'


if __name__=='__main__':
    Py_J = Python_Jenkins_Api_Test(object)
    Py_J.work_with_jenkins_job()
    #Py_J.get_sonar_url()
    Py_J.get_sonar_url_true()

照例

http://python-jenkins.readthedocs.io/en/latest/examples.html



版权声明:本文为Dream_Flying_BJ原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。