问题
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