在Jenkins中列出所有keep-forever构建?

w8rqjzmb  于 12个月前  发布在  Jenkins
关注(0)|答案(3)|浏览(106)

在Jenkins中是否有一种简单的方法来列出所有标记为keep-forever的构建?然后,理想情况下,单击一下就可以取消将构建标记为永久保留或立即删除它?
在我们的过程中,如果一个构建涉及到某种特定类型的失败,我们将其标记为keep-forever;这是为了防止Jenkins随着时间的推移自动删除。我需要一个简单的方法来获得所有那些永久保存的构建的列表,这样它们就不会随着时间的推移占用我们所有的磁盘空间。

uqxowvwt

uqxowvwt1#

以下针对Jenkins的MySQL查询将列出所有标记为“Keep Forever”的构建版本的URL:

http://[jenkins_server]/api/xml?depth=2&xpath=/hudson/job/build[keepLog="true"]/url&wrapper=forever

在浏览器中输入它,看看它会返回什么。
现在,您可以将其嵌入到基于XSLT的HTML中,以获得一个包含这些构建的链接的列表。要删除构建,您可以提供一个调用Jenkins CLI的按钮:

java -jar jenkins-cli.jar -s http://[jenkins_server]/ delete-builds [job-name] [build-num]

不幸的是,我不知道如何禁用'保持建设永远'与CLI不删除它。

js81xvg6

js81xvg62#

我也在寻找同样的东西,我们的Jenkins也很大,并尝试链接:

http://[jenkins_server]/api/xml?depth=2&xpath=/hudson/job/build[keepLog="true"]/url&wrapper=forever

我把它弄坏了。
但事实证明,我一次只需要一个作业的最后一个“永久保存”版本,这似乎工作得更快。因此,我使用以下代码代替:

http://[jenkisn_server]/job/[job_name]/api/xml/?depth=2&xpath=/freeStyleProject/build[keepLog="true"]/number&wrapper=forever

它返回带有标记为“keep forever”的所有构建号的xml。您可以修改xpath以满足您的需要。

jv2fixgn

jv2fixgn3#

您可以使用以下管道来覆盖所有作业及其所有构建,并取消标记构建,如果它是例如。超过90天

def process_job_history(Map config = [:]) {
    // create default dict with some values
    def defaultDict = [
        verbose: false,
        olderThanDays: 30,
    ]

    // update the default map content with the user provided config content
    // new key/value of provided map is automatically added to the defaultDict
    defaultDict << config

    def counter = 0;
    
    // get the job by its name, this requires a non sandboxed job
    def job_name = defaultDict.job_name;
    def job = jenkins.model.Jenkins.instance.getItemByFullName(job_name, Job.class);
    def all_builds = job.getBuilds();

    if (defaultDict.verbose) {
        println();
        println("Processing ${job_name}");
        // println("all_builds: ${all_builds}");
    }

    if (all_builds.size() < 1) {
        if (defaultDict.verbose) {
            println("No builds found");
        }
        return 0;
    }

    def today = new Date();
    def earlier_in_time = today - defaultDict.olderThanDays;

    // collect the history of builds, and print additional infos for each build
    all_builds.each { build ->
        // a value less than 0 if this Date is before the Date argument
        if (build.getTime().compareTo(earlier_in_time) < 0 && build.keepLog) {
            println("${build.absoluteUrl}: ${build.id}, result: ${build.result}, date: ${build.getTime().format('YYYY-MMM-dd HH:mm:ss')}");

            // may "delete" (do not mark it as) keep forever
            // build.keepLog = false;
            
            counter++;
        }
    }

    return counter;
}

pipeline {
    agent any

    stages {
        stage("Process job history") {
            steps {
                // ''' uses system variables like $HOSTNAME, $PWD, ...
                // """ uses groovy variables like someCustomVar
                script {
                    def counter = 0;
                    def job_age = 90;   // 90 days since today

                    Jenkins.instance.getAllItems(Job.class).each {
                        counter += process_job_history([job_name: "${it.getFullName()}", olderThanDays: job_age]);
                    }
                    println("Counted ${counter} jobs older than ${job_age} days");
                }
            }
        }
    }
}

相关问题