TheDauthi TheDauthi - 3 months ago 50
Groovy Question

Programmatically getting Jenkins configuration for a plugin

I'm trying to get (and hopefully change) the Jenkins configuration for a plugin with a Groovy script inside the Groovy console. My specific example is to try to change multiple IP addresses for the publish-over-ssh plugin. It's pretty easy to do via the command line (editing the xml), but after hours of struggling with it, I'd still like to find how it would be done via the groovy console, for no other reason than the enlightenment. I've looked through the Jenkins API javadoc, but to no avail.

How can I find/change the global configuration for a plugin inside the Jenkins console?

Answer

Here is a good place to start searching:

https://github.com/jenkinsci/publish-over-ssh-plugin/tree/master/src/main/java/jenkins/plugins/publish_over_ssh

Here is an example of adding a host:

import jenkins.model.*
import jenkins.plugins.publish_over_ssh.BapSshHostConfiguration
def inst = Jenkins.getInstance()
def publish_ssh = inst.getDescriptor("jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin")
def configuration = new BapSshHostConfiguration(name,
  hostname,
  username,
  encryptedPassword,
  remoteRootDir,
  port,
  timeout,
  overrideKey,
  keyPath,
  key,
  disableExec
)
publish_ssh.addHostConfiguration(configuration)
publish_ssh.save()

Here we can see a couple helpful functions:

https://github.com/jenkinsci/publish-over-ssh-plugin/blob/master/src/main/java/jenkins/plugins/publish_over_ssh/descriptor/BapSshPublisherPluginDescriptor.java

  • getHostConfigurations()
  • removeHostConfiguration(final String name)

Should be all the info you need to do it, cheers!