How to set “Scan Organization Triggers” via Jenkin

2019-07-25 12:14发布

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条回答
▲ chillily
2楼-- · 2019-07-25 12:21

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
查看更多
登录 后发表回答