We’re using Jenkins, we want to create Jenkins plugins and we’re looking at using the jenkins.rb project to create those plugins in Ruby. Very exciting technology that was created by Charles Lowell and Jenkins’ Kohsuke himself.
Jenkins is written in Java, runs on the JVM, and it wasn’t at all obvious how I could "just poke around" a running Jenkins server.
Fortunately, Kohsuke wrote an incredible plugin Pry. Once it is installed, the Jenkins CLI will include a pry
command.
Say you’re running Jenkins server locally, then download the CLI jar file from http://localhost:8080/jnlpJars/jenkins-cli.jar; then run:
$ java -jar jenkins-cli.jar -s http://localhost:8080 pry
[1] pry(Launcher)>
Hurray!
Mapping Jenkins Java to Ruby
You can now navigate around Jenkins.
By default, there is a jenkins
object which represents the Jenkins instance, the root of the object tree. This can also be discovered via Jenkins.getInstance()
within your plugins.
> jenkins
=> #<Java::HudsonModel::Hudson:0x1251dee1>
> Java.jenkins.model.Jenkins.getInstance
=> #<Java::HudsonModel::Hudson:0x1251dee1>
To see how many items (Projects, etc) you have, you can invoke the getItems
method, with or without a filter class. Remember, when invoking the Java methods to pass Java objects/classes (the java_class
method is helpful here).
> jenkins.getItems.size
=> 3
> jenkins.getItems.first
=> #<Java::HudsonModel::FreeStyleProject:0x7e2b2718>
> jenkins.getItems(Java.hudson.model.FreeStyleProject.java_class).size
=> 3
Java.hudson.model.FreeStyleProject
maps to the hudson.model.FreeStyleProject class.
The parameterized Hudson#getItems(javaClass)
actually maps to the Jenkins class’s method.
There is a lot more to discover but at least you now know how to get into a running Jenkins server and explore the object model!
Full Pry console
The local Ruby console is a fully-enabled Pry console.
Please checkout the Pry website for how you can use and extend Pry!