經過python調用jenkins 經常使用api操做

# -*- coding: utf-8 -*-

import jenkins


class TestJenkins(object):
def __new__(cls, *args, **kwargs):
server = 'http://1.1.1.1:8080/jenkins'
username = 'admin'
     # 對應用戶的token信息,不是明文的密碼信息
password = 'fljljdfladoweurojlsjdfasd123'
server = jenkins.Jenkins(url=server, username=username, password=password)
instance = super(TestJenkins, cls).__new__(cls, *args, **kwargs)
instance.server = server
return instance

def __init__(self):
#這裏的api_token是針對遠程執行任務時,jenkins要驗證的token的信息
self.api_token = 'okfine'

def jobs_count(self):
print(self.server.jobs_count())

def server_info(self):
print(self.server.server)

def get_jobs_info(self):
for item in self.server.get_all_jobs():
print('name: %s' % item['name'], 'URL: ', item['url'])

def get_whoami(self):
print(self.server.get_whoami(depth=10))

def get_auth(self):
print(self.server.auth)

def get_debug_job_info(self, name):
print(self.server.debug_job_info(name))

def check_job_exists(self, name):
print(self.server.job_exists(name))

def get_all_jobs(self):
print(self.server.get_all_jobs())

def create_job(self):
print(self.server.create_job('API-1', jenkins.RECONFIG_XML))
print(self.server.create_job('API-2', jenkins.RECONFIG_XML))

def delete_job(self, name):
print(self.server.delete_job(name))

def copy_job(self, name, new_name):
print(self.server.copy_job(name, new_name))

def enable_job(self, name):
print(self.server.enable_job(name))

def disable_job(self, name):
print(self.server.disable_job(name))

def reconfig_job(self, name):
print(self.server.reconfig_job(name, jenkins.RECONFIG_XML))

def rename_job(self, name, new_name):
print(self.server.rename_job(name, new_name))

def build_job(self, name, parameters):
print(self.server.build_job(name, parameters, token=self.api_token))

def get_job_info(self, name):
# 主要是經過任務的信息,來獲得須要傳入的參數信息,jenkins 各類自定義選項信息,單憑肉眼是很難辨別出參數信息的,
# 任務相關參數信息都是在property這個參數裏面
        build_arg = self.server.get_job_info(name)["property"]        for parameter_definitions in build_arg:            if len(parameter_definitions) > 1:                for parameter in parameter_definitions["parameterDefinitions"]:                    print('----------------------------------')                    print('name: %s' % parameter["name"])                    print('class: %s' % parameter["_class"])                    print('type: %s' % parameter["type"])                    print('description: %s' % parameter["description"])                    print('jenkins_arg: %s' % parameter["defaultParameterValue"]["name"])                    print('default_value: %s' % parameter["defaultParameterValue"]["value"])                    if parameter["_class"] == 'hudson.model.ChoiceParameterDefinition':                        print('can_choices: %s' % parameter["choices"])                    print('----------------------------------')        def get_build_log(self, name):        last_build_number = self.server.get_job_info(name)['lastCompletedBuild']['number']        print('last_build_number', last_build_number)        # 查看指定構建編號的輸出        print(self.server.get_build_console_output(name, last_build_number))    def __call__(self, *args, **kwargs):        self.get_job_info('shop')             # 經過特定任務的信息來獲得要傳入的參數信息,而後再來執行構建任務        # build new job, nice  operation        # 以下列所示的5個參數,就是這次構建必須提供的參數        param_dict = {'repository': 'ssh://git@xxxxx.com/xx/xxxx.git',                       'profile': 'xxxx-xxx',                       'branch': 'master',                       'upload_nexus': False,                       'deploy_app': False}        self.build_job('xxxxx', parameters=param_dict)TestJenkins()()相信這篇文章,可以對你們提供幫助,尤爲是經過python來遠程調用jenkins執行構建任務方面
相關文章
相關標籤/搜索