python get Jenkins job and build number info

For multiple services Jenkins job, find a way to get each job and lasted build number for release static, here the way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import jenkins
import json

# Login
s = jenkins.Jenkins(url=URL, username=USERNAME, password=PASSWORD)

# Get all jobs under certain view
jobs = s.get_jobs(view_name=VIEW_NAME)

# Get job and last build number
dic = {}
for job in jobs:
name = job['name']
build = s.get_job_info(name)['lastCompletedBuild']['number']
dic[name] = build
print(json.dumps(dic, indent=4))

Result:

1
2
3
4
5
6
{
"a": 12,
"b": 17,
"c": 20,
"d": 37
}
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!