如何通过Jenkins脚本控制台设置“扫描组织触发器”?

时间:2019-06-04 14:21:07

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-cli

我需要一种通过Jenkins脚本控制台设置“扫描组织触发器”的方法。这很接近,但是仅显示多分支或组织扫描触发器(如果已存在):

https://github.com/cloudbees/jenkins-scripts/blob/master/setOrgFolderIndex.groovy

1 个答案:

答案 0 :(得分:1)

这是我制作的脚本,用于检测多分支或组织触发器。如果项目尚不存在,它将创建一个触发器。注意:addTrigger部分已被注释掉,因此按原样运行应该是安全的。

import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger
import jenkins.model.Jenkins
import jenkins.branch.OrganizationFolder
import hudson.model.Job


projects = [:]
Jenkins.instance.getAllItems(AbstractItem.class).each { j ->
    String jc = j.class.simpleName
    if(!(jc in projects)) {
        projects[jc] = 0
    }
    projects[jc]++
}
println "Count projects by type for: " + Jenkins.getInstance().getRootUrl()
println "    Current Jenkins version: " + Jenkins.getInstance().version
projects.each { type, count ->
    println "    ${type}: ${count}"
}

println "\n-------Organization Items-------"
Jenkins.instance.getAllItems(jenkins.branch.OrganizationFolder.class).each { j ->
    String jobName = j.fullName
    Integer triggerCount = j.triggers.findAll { k,v -> v instanceof com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger}.size()
    if (triggerCount > 0) {
        myInterval = j.triggers.findAll { k,v -> v instanceof com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger}
      .each { k,v -> println "  trigger count: ${triggerCount}  Interval: ${v.getInterval()}, jobName: ${jobName}" }
    }
    else {
        def newInterval = new PeriodicFolderTrigger("1d")
        println "  trigger count: ${triggerCount}  Interval: NULL, jobName: ${jobName}"
        //println "  creating trigger for ${jobName}"
        //j.addTrigger(newInterval)
        //j.save()
    }

}
println "\n-------Multibranch Items-------"
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.class).each { j ->
    String jobName = j.fullName
    Integer triggerCount = j.triggers.findAll { k,v -> v instanceof com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger}.size()
    if (triggerCount > 0) {
        myInterval = j.triggers.findAll { k,v -> v instanceof com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger}
      .each { k,v -> println "  trigger count: ${triggerCount}  Interval: ${v.getInterval()}, jobName: ${jobName}" }
    }
    else {
        def newInterval = new PeriodicFolderTrigger("1d")
        println "  trigger count: ${triggerCount}  Interval: NULL, jobName: ${jobName}"
        //println "  creating trigger for ${jobName}"
        //j.addTrigger(newInterval)
        //j.save()
    }

}

return