How to configure GitHub Organizaiton Item/Job via Groovy

28 views
Skip to first unread message

Phillip Dudley

unread,
Dec 14, 2018, 4:46:05 PM12/14/18
to Jenkins Users
Would anyone have any pointers to configuring the GitHub Organization Item/Job with Groovy so that when my Jenkins instance starts up, it reads the

$JENKINS_HOME/init.groovy.d/

folder and configures a default job to perform GitHub Organization scanning for Jenkinsfiles?

I've got most of the other stuff I need such as setting banners, installing plugins, configuring default Jenkins users and assigning them permissions via the matrix-auth plugin. Even finally hooked up to LDAP and I can authenticate. However, I don't seem to find too much on configuring that plugin except through the UI. Most of my Googling returns results for using Jenkinsfiles, or Groovy tutorials, but not actually setting up and configuring that plugin.

TL;DR,
Looking for a direction to learn how to configure the GitHub Organization job to scan my GitHub Organization for Jenkinsfiles and start performing Jobs.

Mark Waite

unread,
Dec 14, 2018, 7:17:20 PM12/14/18
to Jenkins Users
Since you're already starting from a JENKINS_HOME directory, you may find it easier to store the job definition rather than attempting to create the job from a groovy script.  Refer to https://github.com/MarkEWaite/docker-lfs/blob/lts-slim-with-plugins/ref/jobs/Git-Client-Folder/jobs/git-client-pipeline-github/config.xml for an example in a Docker image.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/e59e3c84-27e5-4c47-a4f9-20e76860ef2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks!
Mark Waite

Phillip Dudley

unread,
Dec 18, 2018, 4:21:52 PM12/18/18
to Jenkins Users
What I ended up doing is using what I had previously to setup Jenkins, login, and install the Job Configuration History plugin, and got the output of the config.xml. For whatever reason, doing the http://JENKINSURL:8080/job/NAME/config.xml would just result in a blank page, and not actually show the configuration of the GitHub Source Branch job.

However, I noticed that the config is tied very specifically to specific versions of Plugins. I can install plugins, but if those change, then they break my job.

/**
 * activatePlugin activates given plugins.
 * The plugin parameter is a pluginManager object(?)
 *
 * @param plugin [Object]
 *
 * @return nil
 */

def activatePlugin(plugin) {
 
// If our specified plugin isn't enabled, enable it.
 
if (! plugin.isEnabled()) {
    plugin
.enable()
   
// Set our state to true.
    deployed
= true
 
}
 
// Go through each dependency of our plugins, and enable those.
 
// Otherwise our plugins wouldn't work even if they're installed.
  plugin
.getDependencies().each {
    activatePlugin
(pluginManager.getPlugin(it.shortName))
 
}
}

// As of 2018-12-18
def plugin_list = [
 
"ant:1.9",
 
"build-timeout:1.19",
 
"email-ext:2.63",
 
"github-branch-source:2.4.1",
 
"gradle:1.29",
 
"ldap:1.20",
 
"matrix-auth:2.3",
 
"antisamy-markup-formatter:1.5",
 
"pam-auth:1.4",
 
"pipeline:2.6",
 
"pipeline-github-lib:1.0",
 
"ssh-slaves:1.29.1",
 
"subversion:2.12.1",
 
"timestamper:1.8.10",
 
"ws-cleanup:0.37",
]

// Loop through all of the plugins and install/update and activate them.
plugin_list
.each { plugin ->
 
// If the plugin isn't being installed, update it.
 
if (! pluginManager.getPlugin(plugin)) {
    deployment
= updateCenter.getPlugin(plugin).deploy(true)
    deployment
.get()
 
}
 
// Activate the plugin and all of its dependencies.
  activatePlugin
(pluginManager.getPlugin(plugin))
}

However, there is an overloaded method in the updateCenter called getPlugin(String name, and VersionNumber version)

I'm not sure where to find the VersionNumber or how to get that to give to this method.

Phillip Dudley

unread,
Dec 18, 2018, 6:55:00 PM12/18/18
to Jenkins Users
Well, now I don't know why my script isn't working. I had to modify it to remove the version numbers, just to get the thing to work. The blog, https://qiita.com/aespinosa/items/5d791310f0cb436eb71f works if I copy and paste their example,

import jenkins.model.Jenkins;

pm
= Jenkins.instance.pluginManager

uc
= Jenkins.instance.updateCenter
pm
.plugins.each { plugin ->
plugin
.disable()
}

deployed
= false
def activatePlugin(plugin) {
if (! plugin.isEnabled()) {
  plugin
.enable()
  deployed
= true
}

plugin
.getDependencies().each {
  activatePlugin
(pm.getPlugin(it.shortName))
}
}

["git", "workflow-aggregator", "github-oauth", "job-dsl", "extended-read-permission"].each {
if (! pm.getPlugin(it)) {
  deployment
= uc.getPlugin(it).deploy(true)
  deployment
.get()
}
activatePlugin
(pm.getPlugin(it))
}

if (deployed) {
Jenkins.instance.restart()
}

but as soon as I add my own Array, I get a nullpointerexception saying that it can't call deploy() with null objects.

import jenkins.model.Jenkins;

pm
= Jenkins.instance.pluginManager

uc
= Jenkins.instance.updateCenter
pm
.plugins.each { plugin ->
  plugin
.disable()
}

deployed
= false
/**
 *
 */

def activatePlugin(plugin) {
 
if (! plugin.isEnabled()) {
    plugin
.enable()
      deployed
= true
 
}

  plugin
.getDependencies().each {
    activatePlugin
(pm.getPlugin(it.shortName))
 
}
}

/** this works.
[
  "git",
  "workflow-aggregator",
  "github-oauth",
  "job-dsl",
  "extended-read-permission"
].each {
  if (! pm.getPlugin(it)) {
    deployment = uc.getPlugin(it).deploy(true)
      deployment.get()
  }
  activatePlugin(pm.getPlugin(it))
}
*/


[
 
"ant",
 
"build-timeout",
 
"credentials",
 
"email-ext",
 
"github-branch-source:",
 
"gradle",
 
"jobConfigHistory",
 
"ldap",
 
"matrix-auth",
 
"antisamy-markup-formatter",
 
"pam-auth",
 
"pipeline",
 
"pipeline-github-lib",
 
"ssh-slaves",
 
"subversion",
 
"timestamper",
 
"ws-cleanup"
].each {
 
if (! pm.getPlugin(it)) {
    deployment
= uc.getPlugin(it).deploy(true)
      deployment
.get()
 
}
  activatePlugin
(pm.getPlugin(it))
}

Currently, it makes no sense. To me those are structured the same, but the second array doesn't work.

Phillip Dudley

unread,
Dec 19, 2018, 5:08:17 PM12/19/18
to Jenkins Users
I figured out the issue, and actually got version pinning down for the Jenkins Plugins.

#!groovy

import jenkins.model.Jenkins
import hudson.util.VersionNumber

// Create a reference to the PluginManager Class.
pm
= Jenkins.instance.pluginManager
// Create a reference to the UpdateCenter Class.
uc
= Jenkins.instance.updateCenter

// Disable all the  plugins on the system first.
pm
.plugins.each { plugin ->
  plugin
.disable()
}

// Used to make sure that the plugins actually installed, and whether or not
// they actually deployed.
deployed
= false

/**
 * activatePlugins is a recursive function that installs and enables the plugins
 * and their dependencies.
 * However, these don't actually do any version pinning, and picks the latest
 * versions of the plugins.

 *
 * @param plugin [Object]
 *
 * @return nil
 */

def activatePlugin(plugin) {
 
if (!plugin.isEnabled()) {

    plugin
.enable()
      deployed
= true
 
}

  plugin
.getDependencies().each {
    activatePlugin
(pm.getPlugin(it.shortName))
 
}
}

[

 
'ant':'1.9',                           // v1.9
 
'build-timeout':'1.19',                // v1.19
 
'credentials':'2.1.18',                // v2.1.18
 
'email-ext':'2.63',                    // v2.63
 
'github-branch-source':'2.4.1',        // v2.4.1
 
'gradle':'1.29',                       // v1.29
 
'jobConfigHistory':'2.19',             // v2.19
 
'ldap':'1.20',                         // v1.20
 
'matrix-auth':'2.3',                   // v2.3
 
'antisamy-markup-formatter': '1.5',    // v1.5
 
'pam-auth':'1.4',                      // v1.4
 
'workflow-aggregator':'2.6',           // v2.6
 
'pipeline-github-lib':'1.0',           // v1.0
 
'ssh-slaves':'1.29.1',                 // v1.29.1
 
'subversion':'2.12.1',                 // v2.12.1
 
'timestamper':'1.8.10',                // v1.8.10
 
'ws-cleanup':'0.37'                    // v0.37
].each { plugin, version ->
 
if (! pm.getPlugin(plugin)) {
   
VersionNumber versionNumber = new VersionNumber(version)
    deployment
= uc.getPlugin(plugin, versionNumber).deploy(true)
    deployment
.get()
 
}
  activatePlugin
(pm.getPlugin(plugin))
}

// Do a simple verification that the plugins did deploy, then restart Jankins
if (deployed) {
 
Jenkins.instance.restart()
}

Now I can pin the version of the plugins, and export the GitHub Source Branch job config and make sure everything works the same. I also need to run the plugin install once, get a list of all the plugins, and then specify those because I'm sure the dependencies of the plugins aren't' very well versioned or are looser than what I'm doing.
Reply all
Reply to author
Forward
0 new messages