一、安装依赖包
pip install python-jenkins
二、常用操作
0、调用jenkins(以下用的server都是这里的环境)
import jenkins
server = jenkins.Jenkins('http://localhost:8080', username='admin', password='123456')
1、获取版本信息
server.get_version()
'''
2.192
'''
2、获取用户信息等
server.get_whoami()
'''
{'_class': 'hudson.model.User', 'absoluteUrl': 'http://10.0.0.141:8080/user/admin', 'description': None, 'fullName': 'qq', 'id': 'admin', 'property': [{'_class': 'jenkins.security.ApiTokenProperty'}, {'_class': 'hudson.plugins.emailext.watching.EmailExtWatchAction$UserProperty', 'triggers': []}, {'_class': 'org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty'}, {'_class': 'hudson.model.PaneStatusProperties'}, {'_class': 'com.cloudbees.plugins.credentials.UserCredentialsProvider$UserCredentialsProperty'}, {'_class': 'hudson.model.MyViewsProperty'}, {'_class': 'hudson.search.UserSearchProperty', 'insensitiveSearch': True}, {'_class': 'hudson.security.HudsonPrivateSecurityRealm$Details'}, {'_class': 'hudson.tasks.Mailer$UserProperty', 'address': 'binzhangvip1@gmail.com'}, {'_class': 'jenkins.security.seed.UserSeedProperty'}, {'_class': 'jenkins.security.LastGrantedAuthoritiesProperty'}]}
'''
3、获取job数量
server.jobs_count()
'''
3
'''
4、获取所有job的详细信息(url、name、状态等)
server.get_jobs()
'''
[{'_class': 'hudson.model.FreeStyleProject', 'name': 'q1', 'url': 'http://10.0.0.141:8080/job/q1/', 'color': 'blue', 'fullname': 'q1'}, {'_class': 'hudson.model.FreeStyleProject', 'name': 'q2', 'url': 'http://10.0.0.141:8080/job/q2/', 'color': 'blue', 'fullname': 'q2'}, {'_class': 'hudson.model.FreeStyleProject', 'name': 'sonar_test', 'url': 'http://10.0.0.141:8080/job/sonar_test/', 'color': 'notbuilt', 'fullname': 'sonar_test'}]
'''
5、获取job的config配置信息(xml格式)
server.get_job_config('q1')
'''
<?xml version='1.1' encoding='UTF-8'?>
<project>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders/>
<publishers/>
<buildWrappers/>
</project>
'''
6、创建job(需指定config配置)
c=server.get_job_config('q1')
#指定的配置为c,job名为q3
server.create_job('q3',c)
7、复制job
#复制q1 job,复制job名为q4
server.copy_job('q1','q4')
8、删除job
#删除q3 job
server.delete_job('q3')
9、构建job
#构建q2 job
server.build_job('q2')
10、获取job当前的构建id
#获取q2 job的最新构建ID
server.get_job_info('q2')['lastCompletedBuild']['number']
'''
1
'''
11、查看job某次build的详细信息
server.get_build_info('q2',1)
'''
{'_class': 'hudson.model.FreeStyleBuild', 'actions': [{'_class': 'hudson.model.CauseAction', 'causes': [{'_class': 'hudson.model.Cause$UserIdCause', 'shortDescription': 'Started by user qq', 'userId': 'admin', 'userName': 'qq'}]}, {}], 'artifacts': [], 'building': False, 'description': None, 'displayName': '#1', 'duration': 50, 'estimatedDuration': 50, 'executor': None, 'fullDisplayName': 'q2 #1', 'id': '1', 'keepLog': False, 'number': 1, 'queueId': 2, 'result': 'SUCCESS', 'timestamp': 1567062080190, 'url': 'http://10.0.0.141:8080/job/q2/1/', 'builtOn': '', 'changeSet': {'_class': 'hudson.scm.EmptyChangeLogSet', 'items': [], 'kind': None}, 'culprits': []}
'''
12、查看视图信息
server.get_views()
'''
[{'_class': 'hudson.model.AllView', 'name': 'all', 'url': 'http://10.0.0.141:8080/'}]
'''
三、编写Jenkins API
# coding:utf-8
import jenkins
class Jenkins_Api:
def __init__(self,username='admin',password='123456'):
self._url = 'http://10.0.0.141:8080'
self._username = username
self._password = password
def get_server(self):
server = jenkins.Jenkins(self._url, username=self._username, password=self._password)
return server
def get_version(self):
return self.get_server.get_version()
def get_jobs(self):
return self.get_server.get_jobs()
def get_jobs_count(self):
return self.get_server.jobs_count()
def get_job_config(self,job_name):
return self.get_server.get_job_config(job_name)
def create_job(self,job_name,config_xml)
return self.get_server.create_job(job_name,config_xml)
def copy_job(self,job_name,new_job_name):
return self.get_server.copy_job(job_name,new_job_name)
def delete_job(self,job_name):
return self.get_server.delete_job(job_name)
def build_job(self,job_name):
return self.get_server.build_job(job_name)
def get_job_info(self,job_name):
return self.get_server.get_job_info(job_name)
def get_job_number(self,job_name):
return self.get_server.get_job_info(job_name)['lastCompletedBuild']['number']
def get_build_info(self,job_name,number):
return self.get_server.get_build_info(job_name,number)
def get_views(self):
return self.get_server.get_views()