如何获取在特定时间运行的Jenkins构建列表?

ruyhziif  于 11个月前  发布在  Jenkins
关注(0)|答案(3)|浏览(132)

我当前的Jenkins有大量的作业。不同的文件夹,每个文件夹都有多个作业。我最近看到一个Jenkins slave(自动缩放)在特定时间向另一个服务器发送了太多请求。但是,如果不手动检查,我无法找到在特定时间运行的构建版本。有没有办法使用API/Groovy脚本来获取此信息?

8mmmxcuj

8mmmxcuj1#

我写了一个非常小的bash脚本在jenkins服务器上运行,用来解析日志文件。
仅仅查看作业的开始时间是不够的。作业可能刚好在您的时间窗口之前开始,甚至在您的时间窗口之后结束;它仍然会在您的时间窗口内运行。

#!/bin/bash
start=$1
end=$2

for build_xml in jobs/*/branches/*/builds/*/build.xml
do
        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"
        fi
done

字符串
使用方法:

./getBuildsRunningBetween.sh 1565535639 1565582439


将给予给予:

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

jei2mxaa

jei2mxaa2#

运行作业可以使用下面的Jenkins API基于颜色分类(称为anime)找到。
“Jenkins host url”/API/xml?tree=jobs[name,url,color]&xpath=/哈德逊/job[ends-with(color/text(),%22_anime%22)]&wrapper=jobs
蓝色的是那些正在运行的
蓝色动画

wrrgggsh

wrrgggsh3#

这可以通过一个简单的python脚本和Jenkins JSON REST API轻松实现。

1.预约

已安装Python 2.7或3.x和python requests库:
第一个月
Python 3.x
pip3 install requests
标签:How to install pip

2.获取日期之间构建的Python脚本

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

字符串
上面的脚本可以在python 2.7和3.x下运行。现在解释一下:
首先使用JSON API下载所有构建数据,并将响应加载为JSON。然后将每个构建的时间戳转换为日期时间,并与开始和停止日期进行比较。请注意,将时间戳除以1000以获得秒而不是毫秒非常重要(否则从时间戳转换的日期将引发ValueError)。
输出示例:

$ 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


另一方面,如果你想以不同的格式提供开始和结束日期,那么记住你需要在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")

3.Python脚本获取构建的确切日期

如果您有兴趣在其确切日期之前查找build,只需替换此行:

if build_date >= start_date and build_date <= stop_date:


用这个:

if build_date == date:


其中date是特定的构建日期。

**请注意!**如果你想找到build by确切的日期,你需要提供date正确的格式。在这种情况下,它是"%d.%m.%Y %H:%M:%S"。示例:

datetime.strptime('10.11.2018 18:46:04', "%d.%m.%Y %H:%M:%S")


否则,即使日期在某个点(分钟,小时,日期等)相同,对于python来说,它也不会是相等的日期。
如果你想提供另一种格式,那么你需要为build_date变量调整它。

build_date.strftime('%d %m,%Y')

相关问题