Reputation: 340
I have a Ruby plugin for Jenkins I'd like to add global configuration to.
The configuration is displayed on Jenkins global configuration page:
Currently, I've managed to save the global configuration by using a custom descriptor. Tried to implement with the serialization done by parent class (Java.hudson.model.Descriptor), like this:
class GitlabWebHookRootActionDescriptor < Jenkins::Model::DefaultDescriptor
attr_reader :conf_param
def configure(req, form)
req.bindJSON(self, form)
save
true
end
end
This solution delegates save to the parent class. The issue with this was:
---- Debugging information ----
message : Could not call org.jruby.RubyClass.writeObject()
cause-exception : java.io.IOException
cause-message : can not serialize singleton object
-------------------------------
Couldn't figure out what the singleton was, so ended up writing my own method to save and load configuration to a file. You can see the code here.
This definitely manages to save the data to an XML file, and reads from it when jenkins starts:
// on startup
INFO: =========== GitlabWebHookRootActionDescriptor initialize ===================
INFO: conf_param: aloha
// on save
INFO: =========== GitlabWebHookRootActionDescriptor configure ===================
INFO: form: {"conf_param"=>"juku"}
But, I can't get the saved configuration to be displayed at the Jenkins configuration page or to be available from the root action itself.
I've added the descriptor to root action with: describe_as Java.hudson.model.Descriptor, :with => GitlabWebHookRootActionDescriptor
but without success.
I can see in the logs that the root action is loaded before the descriptor, don't know if that is the problem.
Does anybody have an idea on how to use the saved configuration in descriptor from the root action?
Upvotes: 4
Views: 474
Reputation: 26
One year after the answer is finally here : jenkins-mysql-job-databases-plugin :)
Basically to get global config anywhere in your plugin :
global_config = Java.jenkins.model.Jenkins.getInstance().getDescriptor(MyGlobalConfigDescriptor.java_class)
It returns the MyGlobalConfigDescriptor
object so you can call methods on to retrive config values.
To display them in the config form :
f.entry :title => 'My global config', :field => 'my_global_config' do
f.textbox :default => "#{descriptor.my_global_config}"
end
Upvotes: 1