Alex Nikitin
Alex Nikitin

Reputation: 941

How can i get all this builds numbers between particular date using python Jenkins API?

For example, i have bunch of builds between 22 and 23. How can i get all this builds numbers between particular date using python Jenkins API?

enter image description here

Upvotes: 2

Views: 2360

Answers (2)

Raoslaw Szamszur
Raoslaw Szamszur

Reputation: 1740

You could do it like so by using Jenkins JSON REST API and python request library:

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)

Above script works both with python 2.7 and 3.x. Now a little explanation:

First download all builds data by using JSON API using requests library (You may need this in order script to work. To install type: pip install requests) 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 
[{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}]

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")

Upvotes: 2

ameydev
ameydev

Reputation: 176

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)

It worked for me. I hope it does for you as well. Thanks :)

Upvotes: 3

Related Questions