Jenkins 如何决定保留构建的天数和保留的最大构建数?

cal*_*oof 3 jenkins

在 Jenkins 作业的“放弃旧版本”设置中,您可以输入“保留版本的天数”和“保留的最大版本数”的值。詹金斯对这两个设置有何反应?它是只选择一个,还是结合行为,以便构建历史记录必须早于时间限制并超过最大构建数量?

Chr*_*lli 5

Jenkins 日志旋转示例

考虑下面的例子

buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '100'))
Run Code Online (Sandbox Code Playgroud)

此日志轮换策略将在每次构建运行,并将删除超过 100 个的任何构建以及超过 30 天的任何构建(即使构建数量少于 100 个)。Jenkins 将查看每个特定构建,如果它仅满足其中一个标准,它将被删除

伪代码

if( build.age > 30 || build.number > 100 ) {
   delete build
}
Run Code Online (Sandbox Code Playgroud)

请记住,这将覆盖您配置的任何全局日志轮换设置

这是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();
        }
    }
Run Code Online (Sandbox Code Playgroud)