How to get a list of Jenkins builds that ran at a particular time? How to get a list of Jenkins builds that ran at a particular time? jenkins jenkins

How to get a list of Jenkins builds that ran at a particular time?


I wrote a very small bash script to run on the jenkins server to parse through the logfiles.

It is not sufficient to look only at the job start time. A job could have started just before your time window and even ended after your time window; it would still have run within your time window.

#!/bin/bashstart=$1end=$2for build_xml in jobs/*/branches/*/builds/*/build.xmldo        startTime=$(sed -n -e "s/.*<startTime>\(.*\)<\/startTime>.*/\1/p" $build_xml)        modificationTime=$(date '+%s' -r $build_xml)        if [[ $modificationTime > $start ]] && [[ $startTime < $end ]]        then                echo "START $(date -d @${startTime::-3})   END $(date -d @$modificationTime)   : $build_xml"        fidone

usage:

./getBuildsRunningBetween.sh 1565535639 1565582439

would give:

START Sun Aug 11 20:29:00 CEST 2019   END Sun Aug 11 20:30:20 CEST 2019   : jobs/job-name/branches/branch-name/builds/277/build.xml


Running jobs can be found based on color categorization called anime, using the below Jenkins API.

"Jenkins host url"/api/xml?tree=jobs[name,url,color]&xpath=/hudson/job[ends-with(color/text(),%22_anime%22)]&wrapper=jobs

Blue ones are the ones that are running

blue_anime


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 requestsfrom datetime import datetimejenkins_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 #21Build number: 21Build url: http://localhost:8080/job/Dummy/21/Build timestamp: 1541875585881Build date: 2018-11-10 18:46:25Job name: Dummy #20Build number: 20Build url: http://localhost:8080/job/Dummy/20/Build timestamp: 1541875564250Build 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')