How to set “Scan Organization Triggers” via Jenkins script console?

冷暖自知 提交于 2019-12-24 11:29:36

问题


I need a way to set “Scan Organization Triggers” via the Jenkins script console. This is close, but only shows Multibranch or Organization Scan triggers if they already exist:

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


回答1:


Here's a script that I made that detects multibranch or organization triggers. If the project doesn't already have one, it will create a trigger. Note: the addTrigger part is commented out, so this should be safe to run as-is.

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


来源:https://stackoverflow.com/questions/56445854/how-to-set-scan-organization-triggers-via-jenkins-script-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!