答案 0 :(得分: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)
对我有用。希望对您也有帮助。 谢谢:)
答案 1 :(得分:1)
您可以使用Jenkins JSON REST API和python request
库来做到这一点:
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。现在进行一些解释:
首先使用requests
库(使用订购脚本可能需要使用JSON API)下载所有构建数据(安装脚本: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 directives.
几个例子:
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")