This can be easily achieved with a simple python script and Jenkins JSON REST API.
1.Prerequisites
Python 2.7 or 3.x and python requests library installed:
pip install requests
For python 3.x
pip3 install requests
Also: How to install pip
2.Python script to fetch builds between dates
import requests
from datetime import datetime
jenkins_url = "JENKINS_HOST"
username = "USERNAME"
password = "PASSWORD"
job_name = "JOB_NAME"
stop_date = datetime.strptime('23.11.2018 0:30:00', "%d.%m.%Y %H:%M:%S")
start_date = datetime.strptime('10.11.2018 18:46:04', "%d.%m.%Y %H:%M:%S")
request_url = "{0:s}/job/{1:s}/api/json{2:s}".format(
jenkins_url,
job_name,
"?tree=builds[fullDisplayName,id,number,timestamp,url]"
)
response = requests.get(request_url, auth=(username, password)).json()
builds = []
for build in response['builds']:
build_date = datetime.utcfromtimestamp(build['timestamp']/1000)
if build_date >= start_date and build_date <= stop_date:
builds.append(build)
print("Job name: {0:s}".format(build["fullDisplayName"]))
print("Build number: {0:d}".format(build["number"]))
print("Build url: {0:s}".format(build["url"]))
print("Build timestamp: {0:d}".format(build["timestamp"]))
print("Build date: {}\n".format(build_date))
Above script works both with python 2.7 and 3.x. Now a little explanation:
First download all builds data by using JSON API and load response as JSON. Then for each build convert its timestamp to date time and compare with start and stop dates. Please note its important to divide timestamp by 1000 to get seconds not milliseconds (otherwise date conversion from timestamp will raise a ValueError).
Example output:
$ python test.py
Job name: Dummy #21
Build number: 21
Build url: http://localhost:8080/job/Dummy/21/
Build timestamp: 1541875585881
Build date: 2018-11-10 18:46:25
Job name: Dummy #20
Build number: 20
Build url: http://localhost:8080/job/Dummy/20/
Build timestamp: 1541875564250
Build date: 2018-11-10 18:46:04
On the other hand, if you want to provide start and stop dates in a different format then remember you'll need to adjust format parameter it in strptime() function. Python datetime directives.
Few examples:
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")
3.Python script to fetch build on exact date
If you are interested in finding build by its exact date just replace this line:
if build_date >= start_date and build_date <= stop_date:
with this:
if build_date == date:
where date is particular build date.
Please note! that if you want to find build by exact date you'll need to provide date with the proper format. In this case it's "%d.%m.%Y %H:%M:%S". Example:
datetime.strptime('10.11.2018 18:46:04', "%d.%m.%Y %H:%M:%S")
Otherwise, even though dates will the same to a certain point (minutes, hours, date etc.) for python it won't be equal dates.
If you want to provide another format then you'll need to adjust it for build_date variable.
build_date.strftime('%d %m,%Y')