通过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执行构建任务方面
posted @ 2019-09-29 15:42  进击的pythoner  阅读(1836)  评论(0编辑  收藏  举报