How do you configure the Jenkins “Create Job Advanced” plugin via groovy?

不羁岁月 提交于 2019-12-19 04:11:27

问题


My main goal is to use groovy to configure Jenkins and all the plugins, so that I don't need to manually configure Jenkins through the web interface, as the the post here was trying.

Specifically, I'm trying to configure the Create Job Advanced plugin in Jenkins through groovy.

When I try this code in the Jenkins Script Console:

import org.codehaus.groovy.runtime.NullObject;

// Jenkins instance
def descriptor = jenkins.model.Jenkins.getInstance().getDescriptorByType(hudson.plugins.createjobadvanced.CreateJobAdvancedPlugin.class);

org.kohsuke.stapler.StaplerRequest stapler = null

// Plugin Parameters
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
jsonObject.put("security", false);
jsonObject.put("jobspacesinname", true);

println ("Is it a stapler request? ${stapler instanceof org.kohsuke.stapler.StaplerRequest}.");

descriptor.configure(stapler, jsonObject); 

I keep getting this error Cannot invoke method configure() on null object and don't know why.

Is it a stapler request? true.
java.lang.NullPointerException: Cannot invoke method configure() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
    at Script1.run(Script1.groovy:14)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:142)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:114)
    at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
    at hudson.util.RemotingDiagnostics.executeGroovy(RemotingDiagnostics.java:111)
    at jenkins.model.Jenkins._doScript(Jenkins.java:3542)
    at jenkins.model.Jenkins.doScript(Jenkins.java:3514)
    at sun.reflect.GeneratedMethodAccessor215.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:298)
    at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:161)
    at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:96)
    at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:121)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:746)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:876)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)
    at org.kohsuke.stapler.Stapler.service(Stapler.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1494)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:123)
    at hudson.plugins.audit_trail.AuditTrailFilter.doFilter(AuditTrailFilter.java:95)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:120)
    at hudson.plugins.greenballs.GreenBallFilter.doFilter(GreenBallFilter.java:58)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:120)
    at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:114)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482)
    at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:48)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
    at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
    at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:168)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482)
    at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482)
    at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:81)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482)
    at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1474)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:499)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:533)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at winstone.BoundedExecutorService$1.run(BoundedExecutorService.java:77)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

How can the Create Job Advanced plugin be configured via groovy?


回答1:


The CreateJobAdvancedPlugin class seems to have no Descriptor. You need to get the plugin instance from the PluginManager instead, e.g.

import hudson.PluginWrapper
import hudson.plugins.createjobadvanced.CreateJobAdvancedPlugin
import jenkins.model.Jenkins

PluginWrapper wrapper = Jenkins.instance.pluginManager.getPlugin(CreateJobAdvancedPlugin)
CreateJobAdvancedPlugin plugin = wrapper.getPlugin() as CreateJobAdvancedPlugin



回答2:


(Posted on behalf of the OP).

Thanks to daspilker, here's the full code that made it work:

import hudson.PluginWrapper
import hudson.plugins.createjobadvanced.CreateJobAdvancedPlugin
import jenkins.model.Jenkins

PluginWrapper wrapper = Jenkins.instance.pluginManager.getPlugin(CreateJobAdvancedPlugin)
CreateJobAdvancedPlugin plugin = wrapper.getPlugin() as CreateJobAdvancedPlugin

org.kohsuke.stapler.StaplerRequest stapler = null

net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
jsonObject.put("security", true);
jsonObject.put("jobspacesinname", true);

plugin.configure(stapler, jsonObject); 

Just add "security", "public", "jobspacesinname", etc as found in the code. SO says I don't have enough reputation points to post more than 2 links. So to see more variables in the CreateJobAdvanced code that processes those variables, go to Github.com site, search for "createdjobadvanced-plugin" to get to the repo.

It's in /blob/352a5876f0d79f990649612a3898501663bb6980/src/main/java/hudson/plugins/createjobadvanced/CreateJobAdvancedPlugin.java#L69-L83. You'll be able to see those relevant variables and you can use add more jsonObject.put("theRelevantVariable", whateverValue); to make changes.

This works on Jenkins 1.609.3 + CreateJobAdvanced 1.8.



来源:https://stackoverflow.com/questions/32826184/how-do-you-configure-the-jenkins-create-job-advanced-plugin-via-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!