Skip to content

Nicholas Duffy

Updating the GitHub Periodic Folder Trigger in Jenkins

jenkins, github, cloudbees1 min read

The GitHub Branch Source Plugin is a plugin for Jenkins, maintained by CloudBees, allows you to organize your multibranch projects and organization folders from GitHub.

The plugin allows you to easily integrate your GitHub repositories in your organization by simply adding some credentials and even filtering based upon your repository names.

One thing that this plugin does by default is scan all of your repositories and builds any branches and PRs that it finds - and by default it does this once per day. This can be undesirable for a number of reasons, a few of which I encountered and so I attempted to turn this feature off.

The catch, however, is that even if you turn off Scan Organization Triggers at the organization level at https://myjenkins/job/myorg/configure, this does not trickle down to the individual repositories (why is this option even there, then?).

In addition, you can't actually turn off this feature or set the interval to perform the scans at the repository level. You can only View Configuration for each repository, which doesn't allow any editing.

So how do we turn this thing off? Well, I couldn't manage that, but was able to switch the interval to the maximum 28 days, so at the very least I don't have to deal with this too often.

You can run the below in https://myjenkins/script in order to change the interval.

WARNING: This will change settings in your Jenkins instance.

1// Update the PeriodicFolderTrigger of each job inside of a Cloudbees folder.
2// Useful for updating individual repos as you cannot do this through the UI.
3// https://stackoverflow.com/questions/57077851/jenkins-github-plugin-scan-organization-triggers
4import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger
5import jenkins.model.Jenkins
6import jenkins.branch.OrganizationFolder
7
8println "Multibranch Items\n-------"
9Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.class).each { it.triggers
10 .findAll { k,v -> v instanceof com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger }
11 .each { k,v -> setInterval(it) }
12}
13
14def setInterval(folder) {
15 println "[INFO] : Updating ${folder.name}... "
16 folder.getTriggers().find {triggerEntry ->
17 def key = triggerEntry.key
18 if (key instanceof PeriodicFolderTrigger.DescriptorImpl){
19 println "[INFO] : Current interval : " + triggerEntry.value.getInterval()
20 // Valid intervals are here:
21 // https://github.com/jenkinsci/cloudbees-folder-plugin/blob/master/src/main/java/com/cloudbees/hudson/plugins/folder/computed/PeriodicFolderTrigger.java#L261-L278
22 def newInterval = new PeriodicFolderTrigger("28d")
23 folder.addTrigger(newInterval)
24 folder.save()
25 println "[INFO] : New interval : " + newInterval.getInterval()
26 }
27 }
28}