使用jenkins json rest api获取(结果中不包含每次的构建结果)

import datetime
import time

from datetime import datetime

import requests


# Provide following data:
from requests.auth import HTTPBasicAuth

jenkins_url = "YOUR_JENKINS_URL"
username = "USERNAME"
password = "PASSWORD"
job_name = "JOBNAME"
stop_date = datetime.strptime("18.8.2021 18:30", "%d.%m.%Y %H:%M")
start_date = datetime.strptime("18.8.2021 10:30", "%d.%m.%Y %H:%M")


def getBuilds():
    # 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]"
    )
    print(request_url)
    response = requests.get(request_url, auth=HTTPBasicAuth(username.encode('utf-8'), password.encode('utf-8'))).json()
    builds = []

    for build in response["builds"]:
        # Convert build timestamp to datetime
        build_date = timeStamp(build["timestamp"])

        # Compare build datetime with provided dates range
        if start_date < build_date < stop_date:
            # Do stuff with builds which fits dates range
            builds.append(build)
    return builds


def timeStamp(timeNum):
    timeStamp = float(timeNum/1000)
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return datetime.strptime(otherStyleTime, "%Y-%m-%d %H:%M:%S")


if __name__ == '__main__':
    print(getBuilds())

Q.E.D.


Nothing really matters to me!