Jenkins 如何在保留构建的天数和要保留的最大构建数量之间做出决定?
Posted
技术标签:
【中文标题】Jenkins 如何在保留构建的天数和要保留的最大构建数量之间做出决定?【英文标题】:How does Jenkins decide between days to keep builds and max number of builds to keep? 【发布时间】:2021-11-16 16:05:47 【问题描述】:在 Jenkins 作业的“放弃旧版本”设置中,您可以为“保留构建的天数”和“要保留的最大构建数量”输入一个值。 Jenkins 对这两个设置有何反应?它只是选择一个,还是结合行为以使构建历史记录必须早于时间限制并超过最大构建数?
【问题讨论】:
【参考方案1】:Jenkins 日志循环示例
考虑以下示例
buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '100'))
此日志轮换策略将在每次构建之后运行,它将删除任何超过 100 天的构建以及超过 30 天的任何构建(即使构建少于 100 天) . Jenkins 将查看每个特定的构建,如果它只满足其中一个标准,它将被删除
伪代码
if( build.age > 30 || build.number > 100 )
delete build
请记住,这将覆盖您已配置的任何全局日志轮换设置
这是 Jenkins 核心使用的 LogRotator 类。这是 Jenkins 用来执行日志轮换的确切代码
if(numToKeep!=-1)
// Note that RunList.size is deprecated, and indeed here we are loading all the builds of the job.
// However we would need to load the first numToKeep anyway, just to skip over them;
// and we would need to load the rest anyway, to delete them.
// (Using RunMap.headMap would not suffice, since we do not know if some recent builds have been deleted for other reasons,
// so simply subtracting numToKeep from the currently last build number might cause us to delete too many.)
RunList<? extends Run<?,?>> builds = job.getBuilds();
for (Run r : builds.subList(Math.min(builds.size(), numToKeep), builds.size()))
if (shouldKeepRun(r, lsb, lstb))
continue;
LOGGER.log(FINE, "0 is to be removed", r);
try r.delete();
catch (IOException ex) exceptionMap.computeIfAbsent(r, key -> new HashSet<>()).add(ex);
if(daysToKeep!=-1)
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR,-daysToKeep);
Run r = job.getFirstBuild();
while (r != null)
if (tooNew(r, cal))
break;
if (!shouldKeepRun(r, lsb, lstb))
LOGGER.log(FINE, "0 is to be removed", r);
try r.delete();
catch (IOException ex) exceptionMap.computeIfAbsent(r, key -> new HashSet<>()).add(ex);
r = r.getNextBuild();
【讨论】:
感谢您的回复!您在哪里找到这些信息? @calebwoof 我不一定在任何地方都能找到它。我知道是因为我的全职工作是和 Jenkins 一起工作。但是,我更新了我的原始答案以包含控制此过程的 Java 类,以便您可以自己检查代码以上是关于Jenkins 如何在保留构建的天数和要保留的最大构建数量之间做出决定?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 cloudformation 或 yaml 将保留天数设置为日志流