删除Jenkins中的构建历史记录

时间:2017-03-17 04:32:34

标签: jenkins groovy

我尝试使用此答案中的说明从Jenkins中删除构建历史记录:

How do I clear my Jenkins/Hudson build history?

但是,我收到以下错误消息:

  

groovy.lang.MissingMethodException:没有方法签名:   jenkins.branch.OrganizationFolder.getBuilds()适用于   参数类型:()值:[]可能的解决方案:getViews(),   doBuild(jenkins.util.TimeDuration),getUrl(),getClass(),   getActions(),getApi()at   org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)       在   org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)       在   org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)       在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)       在   org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)

如果我运行此脚本:

def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)
println(job)

我得到以下输出:

  

jenkins.branch.OrganizationFolder@134f3a3c [github上测试]

我使用的是版本2.32.2。

这是我运行的确切脚本:

def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()

2 个答案:

答案 0 :(得分:4)

您尝试删除的作业github-test是一个组织文件夹,例如你得到jenkins.branch.OrganizationFolder。这意味着它不是实际构建的实际工作,而是包含其他工作的文件夹。值得注意的是OrganizationFolder属于ComputedFolder类型,这意味着当Jenkins找到新的存储库和分支(在github上)时会自动填充它。

所以我猜你要删除github-test作业/文件夹中子作业的所有版本。我修改了链接的答案,以便对帐户进行说明:

import com.cloudbees.hudson.plugins.folder.AbstractFolder
import hudson.model.AbstractItem

// change this variable to match the name of the job whose builds you want to delete
def jobName = "test"
// Set to true in order to reset build number to 1
def resetBuildNumber = false

def removeBuilds(job, resetBuildNumber) {
  if (job instanceof AbstractFolder) {
    for (subJob in job.getItems()) {
      removeBuilds(subJob, resetBuildNumber)
    }
  } else if (job instanceof AbstractItem) {
    job.getBuilds().each { it.delete() }
    if (resetBuildNumber) {
      job.nextBuildNumber = 1
      job.save()
    }
  } else {
    throw new RuntimeException("Unsupported job type ${job.getClass().getName()}!")
  }
}
removeBuilds(Jenkins.instance.getItem(jobName), resetBuildNumber)

答案 1 :(得分:2)

您可以使用以下脚本删除构建。您可以指定要保留的最新构建数量,只需将您选择的值添加到MAX_BUILDS。将其设置为0以删除所有内容。

假设您要删除的构建属于" github-test"。用您的选择替换jobName值

MAX_BUILDS = 5
def jobName = "github-test"
def job = Jenkins.instance.getItem(jobName)

println ""

println "selected Jenkins Job : "
println job.name

def recent = job.builds.limit(MAX_BUILDS)
println recent

  for (build in job.builds) {
    if (!recent.contains(build)) {
      println ""
      println "========================================================="
      println "Preparing to delete: " + build
      build.delete()
    println ""
    }
  }