python3 jenkins api操作

邮差的信 提交于 2019-11-28 22:20:44

一、安装依赖包

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/'}]
'''

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!