例如,我有一堆 22 到 23 之间的构建。如何使用 python Jenkins API 在特定日期之间获取所有这些构建编号?
问问题
1442 次
2 回答
1
你可以通过使用 Jenkins JSON REST API 和 pythonrequest
库来做到这一点:
import requests
from datetime import datetime
# Provide following data:
jenkins_url = "YOUR_JENKINS_URL"
username = "USERNAME"
password = "PASSWORD"
job_name = "JOBNAME"
stop_date = datetime.strptime("23.11.2018 0:30", "%d.%m.%Y %H:%M")
start_date = datetime.strptime("22.11.2018 17:30", "%d.%m.%Y %H:%M")
# Downloading all builds data in one request
request_url = "{0:s}/job/{1:s}/api/json{2:s}".format(
jenkins_url,
job_name,
"?tree=builds[fullDisplayName,id,number,timestamp]"
)
response = requests.get(request_url, auth=(username, password)).json()
builds = []
for build in response["builds"]:
# Convert build timestamp to datetime
build_date = datetime.utcfromtimestamp(build["timestamp"]/1000)
# Compare build datetime with provided dates range
if build_date > start_date and build_date < stop_date:
# Do stuff with builds which fits dates range
builds.append(build)
print(builds)
上面的脚本适用于 python 2.7 和 3.x。现在稍微解释一下:
首先使用库使用 JSON API 下载所有构建数据requests
(您可能需要这个才能使脚本工作。要安装类型pip install requests
:)并将响应加载为 JSON。然后对于每个构建将其时间戳转换为日期时间并与开始和停止日期进行比较。请注意将时间戳除以 1000 以获得秒而不是毫秒(否则从时间戳转换日期将引发 ValueError),这一点很重要。
示例输出:
$ python test.py
[{u'timestamp': 1541875585881, u'_class': u'hudson.model.FreeStyleBuild', u'number': 21, u'fullDisplayName': u'Dummy #21', u'id': u'21'}, {u'timestamp': 1541875564250, u'_class': u'hudson.model.FreeStyleBuild', u'number': 20, u'fullDisplayName': u'Dummy #20', u'id': u'20'}, {u'timestamp': 1541875506564, u'_class': u'hudson.model.FreeStyleBuild', u'number': 19, u'fullDisplayName': u'Dummy #19', u'id': u'19'}, {u'timestamp': 1541875472100, u'_class': u'hudson.model.FreeStyleBuild', u'number': 18, u'fullDisplayName': u'Dummy #18', u'id': u'18'}]
$ python3 test.py
[{'_class': 'hudson.model.FreeStyleBuild', 'fullDisplayName': 'Dummy #21', 'id': '21', 'number': 21, 'timestamp': 1541875585881}, {'_class': 'hudson.model.FreeStyleBuild', 'fullDisplayName': 'Dummy #20', 'id': '20', 'number': 20, 'timestamp': 1541875564250}, {'_class': 'hudson.model.FreeStyleBuild', 'fullDisplayName': 'Dummy #19', 'id': '19', 'number': 19, 'timestamp': 1541875506564}, {'_class': 'hudson.model.FreeStyleBuild', 'fullDisplayName': 'Dummy #18', 'id': '18', 'number': 18, 'timestamp': 1541875472100}]
另一方面,如果您想以不同的格式提供开始和停止日期,请记住您需要在strptime()
函数中调整格式参数。
Python 日期时间指令。
几个例子:
datetime.strptime("23.11.2018", "%d.%m.%Y")
datetime.strptime("2018.11.23", "%Y.%m.%d")
datetime.strptime("Jun 1 2005 1:33PM", "%b %d %Y %I:%M%p")
于 2018-11-26T20:35:16.607 回答
1
import jenkins
from datetime import datetime
jenkins_url=''
username=''
password=''
job_name=''
# Fill up your dates in below fields.
startDate = int(datetime(2018, 10, 20).strftime('%s'))
endDate = int(datetime(2018, 11, 25).strftime('%s'))
server = jenkins.Jenkins(jenkins_url,username,password)
job_info=server.get_job_info(job_name)
#get the number of finished builds of the job
total_builds=job_info['lastBuild']['number']
for build_number in range(1, total_builds):
# get build info for every build number.
build_info = server.get_build_info(job_name, build_number)
timestamp=build_info['timestamp']
timestamp=timestamp/1000 # The timestamp returned by Jenkins api is in miliseconds
build_date=datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
if (timestamp > startDate) & (timestamp < endDate):
print 'Build Time: '+str(build_date)
它对我有用。我希望它也适合你。谢谢 :)
于 2018-11-23T14:22:55.443 回答