您的位置:首页 > 编程语言 > Python开发

通过jenkins-Python在后台操作Jenkins构建job

2020-01-14 18:23 811 查看

  最近要开发1个接口,接收到1个指令后自动触发自动化测试,虽然也可以通过shell命令做这一步,但因为目前所有构建自动化的的动作都通过jenkins完成,所以想要尝试能不能用python去控制jenkins构建job。还真有!万能的python。想起来一句话,有趣的事,python永远不会缺席!

 

  通过jenkins-python实现在后台操作jenkins构建job,只需要5步,并且前面4步都是简单的配置,用python代码实现操作也只有不到20行代码,很简单

  step1:在jenkins中创建1个测试job

  step2:查看Jenkins的User Id,并记录下来,等下配置python脚本时需要用到

 

  step3:jenkins生成api-token,记录token至1个文档中,后面要用到 

 

 

点击【保存】

 

  step4:安装python-jenkins库

  命令行安装

1 sudo pip install python-jenkins

  pythonCharm安装:

  当前使用的python-jenkins版本是1.5.0 

   接下来通过python脚本尝试构建一个jenkins项目

  代码示例:

   通过这个小示例,感觉用python-jenkins还是可以非常方便在的在后台操作jenkins的。

  

1 #导入依赖
2 import jenkins
3
4 #定义远程的jenkins master server 的url,以及port
5 jenkins_server_url = 'http://10.1.71.51:8080/jenkins'
6
7 #定义用户的Userid 和 api token
8 user_id = 'admin'
9 api_token = '11a7a8151dbde5173fa19b346ad46b5efe'
10
11 #实例化jenkins对象,连接远程的jenkins master server
12 server = jenkins.Jenkins(jenkins_server_url,username=user_id,password=api_token)
13 #打印一下server查是否连接成功
14 # print(server) #返回一个jenkins对象<jenkins.Jenkins object at 0x10807d190>
15
16 # 构建job名为testJob的job(不带构建参数)
17 # server.build_job('testJob')
18
19 #查看某个job的构建信息
20 job_info=server.get_job_info('testJob')
21 # print(job_info)
22
23 #获取job名为testJob的job的最后次构建号
24 lastbuildNumber=server.get_job_info('testJob')['lastBuild']['number']
25
26 #获取job名为testJob的job的最后1次构建的执行结果状态
27 result =server.get_build_info('testJob',lastbuildNumber)['result']
28
29 #判断testJob是否还在构建中
30 status = server.get_build_info('testJob',lastbuildNumber)['building']
31 print(status)

 

 

 

  我们可以打印job_info可以一下会看到什么信息:

  可以很明显看出来,get_job_info('job_name')返回的是整个job的构建信息

job_info=server.get_job_info('testJob')

 

1 {
2     '_class': 'hudson.model.FreeStyleProject',
3     'actions': [
4         {
5
6         },
7         {
8
9         },
10         {
11
12         },
13         {
14             '_class': 'com.cloudbees.plugins.credentials.ViewCredentialsAction'
15         }
16     ],
17     'description': '',
18     'displayName': 'testJob',
19     'displayNameOrNull': None,
20     'fullDisplayName': 'testJob',
21     'fullName': 'testJob',
22     'name': 'testJob',
23     'url': 'http: //10.1.71.51/jenkins/job/testJob/',
24     'buildable': True,
25     'builds': [
26         {
27             '_class': 'hudson.model.FreeStyleBuild',
28             'number': 25,
29             'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
30         },
31         {
32             '_class': 'hudson.model.FreeStyleBuild',
33             'number': 24,
34             'url': 'http: //10.1.71.51/jenkins/job/testJob/24/'
35         },
36         {
37             '_class': 'hudson.model.FreeStyleBuild',
38             'number': 23,
39             'url': 'http: //10.1.71.51/jenkins/job/testJob/23/'
40         }
41     ],
42     'color': 'blue',
43     'firstBuild': {
44         '_class': 'hudson.model.FreeStyleBuild',
45         'number': 1,
46         'url': 'http: //10.1.71.51/jenkins/job/testJob/1/'
47     },
48     'healthReport': [
49         {
50             'description': 'Buildstability: Norecentbuildsfailed.',
51             'iconClassName': 'icon-health-80plus',
52             'iconUrl': 'health-80plus.png',
53             'score': 100
54         }
55     ],
56     'inQueue': True,
57     'keepDependencies': False,
58     'lastBuild': {
59         '_class': 'hudson.model.FreeStyleBuild',
60         'number': 25,
61         'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
62     },
63     'lastCompletedBuild': {
64         '_class': 'hudson.model.FreeStyleBuild',
65         'number': 25,
66         'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
67     },
68     'lastFailedBuild': {
69         '_class': 'hudson.model.FreeStyleBuild',
70         'number': 7,
71         'url': 'http: //10.1.71.51/jenkins/job/testJob/7/'
72     },
73     'lastStableBuild': {
74         '_class': 'hudson.model.FreeStyleBuild',
75         'number': 25,
76         'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
77     },
78     'lastSuccessfulBuild': {
79         '_class': 'hudson.model.FreeStyleBuild',
80         'number': 25,
81         'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
82     },
83     'lastUnstableBuild': None,
84     'lastUnsuccessfulBuild': {
85         '_class': 'hudson.model.FreeStyleBuild',
86         'number': 7,
87         'url': 'http: //10.1.71.51/jenkins/job/testJob/7/'
88     },
89     'nextBuildNumber': 26,
90     'property': [
91
92     ],
93     'queueItem': {
94         '_class': 'hudson.model.Queue$WaitingItem',
95         'blocked': False,
96         'buildable': False,
97         'id': 55,
98         'inQueueSince': 1567674112446,
99         'params': '',
100         'stuck': False,
101         'task': {
102             '_class': 'hudson.model.FreeStyleProject',
103             'name': 'testJob',
104             'url': 'http: //10.1.71.51/jenkins/job/testJob/'
105         },
106         'url': 'queue/item/55/',
107         'why': 'Inthequietperiod.Expiresin4.9秒',
108         'timestamp': 1567674117446
109     },
110     'concurrentBuild': False,
111     'downstreamProjects': [
112
113     ],
114     'labelExpression': None,
115     'scm': {
116         '_class': 'hudson.scm.NullSCM'
117     },
118     'upstreamProjects': [
119
120     ]
121 }

 

 参考文档:

Python-Jenkins API使用 —— 在后端代码中操控Jenkins

 

转载于:https://www.cnblogs.com/kaerxifa/p/11466976.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
aff60756 发布了0 篇原创文章 · 获赞 0 · 访问量 5194 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: