Some Jenkins builds are missed when fetching jobs completed after a given timestamp using Groovy script

Hi everyone,

I’m running a Groovy script in Jenkins to fetch all completed builds after a given timestamp “start” so that I can store them in a database. Find the groovy script below:

def jobs = Jenkins.instance.getAllItems(FreeStyleProject)
def builds = jobs.collectMany { job →
job.getBuilds().completedOnly().findAll { build →
long completedAt = build.getStartTimeInMillis() + build.duration
completedAt >= start
}
}

echo “Nr of builds in timeframe: ${builds.size()}”

writeJSON file: ‘results.json’, json: builds.collect {
[
url           : it.url,
test_plan_name: getFolderPropertyValue(it, “SOURCE_BRANCH”) ?: it.parent.fullName.split(‘/’)[1],
test_plan_id  : getFolderPropertyValue(it, “DTA_PORTAL_TEST_PLAN_ID”),
job           : it.parent.fullName,
build_nr      : it.number,
result        : it.result.toString(),
timestamp     : new Date(it.getStartTimeInMillis() + it.duration)
.format(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z’”),
duration      : it.duration,
built_on      : it.getBuiltOnStr(),
]
}

The script doesn’t seem to include all builds that should be within the given time range — some are consistently missing.

Why might certain builds be missing from the result, even though their completion timestamps fall after start?

Any insight or example on a more robust way to collect all completed builds after a timestamp would be very helpful.

Thanks in advance!