1、引言:

现阶段大部分公司,在日常运维中使用jenkins频率还是相对多,今天我们简单探讨下jenkins api的使用。

2、Python库

2.1、python-jenkins

python-jenkins官方链接:
http://python-jenkins.readthedocs.io/en/latest/examples.html#example-1-get-version-of-jenkins

2.2、jenkinsapi

jenkinsapi 官方链接:
https://pypi.org/project/jenkinsapi/#description

3、python-jenkins库

3.1安装

1
2
3
pip install python-jenkins
# 或者
easy_install python-jenkins

3.2案例

3.2.1 获取版本号

1
2
3
4
5
import jenkins

server = jenkins.Jenkins('http://localhost:8080', username='admin', password='admin')

user = server.get_whoami()

version = server.get_version()

print('Hello %s from Jenkins %s' % (user['fullName'], version))

3.2.2 简单API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# coding:utf-8
import jenkins

class Jenkins_Api(object):
def __init__(self):
self._url='http://192.168.72.128:8080'
self._username="admin"
self._password="admin"
def get_server_instance(self):
server=jenkins.Jenkins(self._url, username=self._username, password=self._password)
user=server.get_whoami()
return server

def get_version(self):
return self.get_server_instance().get_version()

def get_jobs(self):
return {
"jobs_count":self.get_server_instance().jobs_count(),
"get_jobs":self.get_server_instance().get_jobs()}

def create_job(self,job_name):
return self.get_server_instance().create_job(job_name)
def get_job_confing(self,job_name):
return self.get_server_instance().get_job_config(job_name)

def copy_job(self,job_name,new_job_name):
return self.get_server_instance().copy_job(job_name,new_job_name)

def build_job(self,job_name,parameters=None,):
return self.get_server_instance().build_job(job_name,parameters=parameters)

def delete_build(self,job_name,number):
return self.get_server_instance().delete_build(job_name,number)

def get_job_info(self,job_name):
return self.get_server_instance().get_job_info('test')['lastCompletedBuild']['number']

def get_build_info(self,job_name,number):
return self.get_server_instance().get_build_info(job_name, number)

def get_build_console_output(self, job_name, number):
return self.get_server_instance().get_build_console_output(job_name, number)

def create_view(self,view_name):
return self.get_server_instance().create_view(view_name,config_xml=jenkins.EMPTY_VIEW_CONFIG_XML)

def get_views(self):
return self.get_server_instance().get_views()

if __name__=='__main__':
print(Jenkins_Api().get_views())
# print(Jenkins_Api().get_build_console_output(job_name='test',number=7))
print(Jenkins_Api().build_job(job_name='test',parameters={"Branch":"origin/master"}))

4、jenkinsapi库

4.1安装

1
2
3
pip install python-jenkins
# 或者
easy_install python-jenkins

4.2案例

4.2.1获取版本号

1
2
3
4
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
J=Jenkins('http://192.168.72.128:8080',username="admin",password="admin")
print(J.version)

4.2.2 获取Job详细输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Jenkins server
from jenkinsapi.jenkins import Jenkins

def get_server_instance():
jenkins_url = 'http://jenkins_host:8080'
server = Jenkins('http://192.168.72.128:8080',username="admin",password="admin")
return server
def get_job_details():
# Refer Example #1 for definition of function 'get_server_instance'
server = get_server_instance()
for job_name, job_instance in server.get_jobs():
print("-"*20)
print('Job Name:%s' % (job_instance.name))
print('Job Description:%s' % (job_instance.get_description()))
print('Is Job running:%s' % (job_instance.is_running()))
print('Is Job enabled:%s' % (job_instance.is_enabled()))

4.2.3 获取最新构建输出

1
2
3
4
5
6
7
8
9
10
11
12
import jenkinsapi
from jenkinsapi.jenkins import Jenkins

def get_server_instance():
jenkins_url = 'http://jenkins_host:8080'
server = Jenkins('http://192.168.72.128:8080',username="admin",password="admin")
return server

def get_last_console():
server = get_server_instance()
for i in server.keys():
print(server[i].get_last_completed_build().get_console())

4.2.4 简单API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from jenkinsapi.jenkins import Jenkins
import json

class Jenkins_Api(object):
def __init__(self):
self._url='http://192.168.72.128:8080'
self._username="admin"
self._password="admin"
def get_server_instance(self):
server = Jenkins(self._url, username=self._username, password=self._password)
return server

def get_version(self):
return self.get_server_instance().version

def get_job_last_console(self,job_name):
data={
'time': str(self.get_server_instance()[job_name].get_last_completed_build().get_timestamp()),
'status':self.get_server_instance()[job_name].get_last_completed_build().get_status(),
'res':self.get_server_instance()[job_name].get_last_completed_build().get_console()
}
return data
#return json.dumps(data)

if __name__=='__main__':
print(Jenkins_Api().get_job_last_console(job_name='test'))

Comments

2019-02-17