Groovy script to do jenkins configuration

4,996 views
Skip to first unread message

Ankush Aggarwal

unread,
Nov 10, 2014, 7:29:53 AM11/10/14
to jenkins...@googlegroups.com
Hi All,

I have installed sonar and artifactory plugin in jenkins. I want to do both sonar and artifactory configurations via groovy script.

I am able to do using GUI Manage Jenkins -> configure system but need to do similar activity via groovy script.

Anyone please help me on this.

Thanks,
Ankush

Rob Mandeville

unread,
Nov 10, 2014, 8:33:44 AM11/10/14
to jenkins...@googlegroups.com

If you want to configure your Jenkins service via Groovy, you need the Groovy plugin.  This allows you to run a “System Groovy Script”.  This runs in the same JVM as the server.

 

This gives you the raw capabilities.  I don’t know of one particular reference to tell you how to do what you’re trying to do.  You may need to get the javadoc for your server and for the plugins you wish to configure.  I don’t have the code here, but at a previous position I was able to use this capability to enable and disable slave nodes as part of a Jenkins job.

 

You may also want to look at the Scriptler plugin.  This will allow easier storage of your Groovy configuration scripts, and easier execution of them within a Jenkins job.

 

Hope this helps!

 

--Rob Mandeville

--
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.
For more options, visit https://groups.google.com/d/optout.

Click here to report this email as spam.



This e-mail and the information, including any attachments it contains, are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.

Thank you.

Please consider the environment before printing this email.

Ankush Aggarwal

unread,
Nov 10, 2014, 9:05:19 AM11/10/14
to jenkins...@googlegroups.com, rmand...@dekaresearch.com
Hi Rob,

I have script to add Maven configuration through groovy script and it is working fine. i need similar kind of script for sonar and artifactory.

maven3.groovy


import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
a=Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0];
b=(a.installations as List);
b.add(new hudson.tasks.Maven.MavenInstallation("MAVEN3", "/home/jenkins/apache-maven/apache-maven-3.2.3", []));
a.installations=b
a.save()

command line to execute.
java -jar jenkins-cli.jar -s  http://localhost:8080/ groovy maven3.groovy

-Ankush

Rob Mandeville

unread,
Nov 10, 2014, 9:32:58 AM11/10/14
to jenkins...@googlegroups.com

Sorry, I don’t know how to help you more there.

 

--Rob

Ankush Aggarwal

unread,
Nov 11, 2014, 3:11:49 AM11/11/14
to jenkins...@googlegroups.com, rmand...@dekaresearch.com
Resolved... Both scripts are ready to use...

===========================
Sonar Script
==========================
import jenkins.model.*
import hudson.plugins.sonar.*
import hudson.plugins.sonar.model.*

def inst = Jenkins.getInstance()

def desc = inst.getDescriptor("hudson.plugins.sonar.SonarPublisher")

def sinst = new SonarInstallation(
  "sonar4.5.1",
  false,
  "jdbc:mysql://localhost:3306/sonar",
  "com.mysql.jdbc.Driver",
  "sonar",
  "sonar",
  "",
  "-Dsonar.sourceEncoding=\"UTF-8\"",
  new TriggersConfig(),
  "admin",
  "admin"
)
desc.setInstallations(sinst)

desc.save()


==================================================
Artifactory Script
==================================================
import jenkins.model.*
import org.jfrog.*
import org.jfrog.hudson.*
import org.jfrog.hudson.util.Credentials;

def inst = Jenkins.getInstance()

def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")

def deployerCredentials = new Credentials("admin", "password")
def resolverCredentials = new Credentials("", "")

def sinst = [new ArtifactoryServer(
  "server-id",
  deployerCredentials,
  resolverCredentials,
  300,
  false )
]

desc.setArtifactoryServers(sinst)

desc.save()

Anurag Sernapally

unread,
Feb 2, 2016, 5:03:58 AM2/2/16
to Jenkins Users
Hi Ankush,

         I have installed sonar plugin using "jenkins_plugin" but while configuring it with groovy scripts I am getting this error. I am pretty new to chef so can you help me understand what I am missing. I am using same code as you have suggested above


    ============================================================================
====
    Error executing action `execute` on resource 'jenkins_script[sonar]'
    ============================================================================
====

    Mixlib::ShellOut::ShellCommandFailed
    ------------------------------------
    Expected process to exit with [0], but received '255'
    ---- Begin output of "java" -jar "/var/chef/cache/jenkins-cli.jar" -s http:/
/localhost:8080 groovy /tmp/groovy20160202-14365-1vd8grq ----
    STDOUT:
    STDERR: Unexpected exception occurred while performing groovy command!
    groovy.lang.GroovyRuntimeException: Could not find matching constructor for:
 hudson.plugins.sonar.SonarInstallation(java.lang.String, java.lang.Boolean, jav
a.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.S
tring, java.lang.String, java.lang.String, hudson.plugins.sonar.model.TriggersCo
nfig, java.lang.String, java.lang.String)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1491)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1407)
        at org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite.callCon
structor(MetaClassConstructorSite.java:46)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstru
ctor(CallSiteArray.java:54)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor
(AbstractCallSite.java:182)
        at RemoteClass.run(RemoteClass:20)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.j
ava:266)
        at groovy.lang.GroovyShell.run(GroovyShell.java:517)
        at hudson.cli.GroovyCommand.run(GroovyCommand.java:86)
        at hudson.cli.CLICommand.main(CLICommand.java:238)
        at hudson.cli.CliManagerImpl.main(CliManagerImpl.java:92)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at hudson.remoting.RemoteInvocationHandler$RPCRequest.perform(RemoteInvo
cationHandler.java:608)
        at hudson.remoting.RemoteInvocationHandler$RPCRequest.call(RemoteInvocat
ionHandler.java:583)
        at hudson.remoting.RemoteInvocationHandler$RPCRequest.call(RemoteInvocat
ionHandler.java:542)
        at hudson.remoting.UserRequest.perform(UserRequest.java:120)
        at hudson.remoting.UserRequest.perform(UserRequest.java:48)
        at hudson.remoting.Request$2.run(Request.java:326)
        at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecut
orService.java:68)
        at hudson.cli.CliManagerImpl$1.call(CliManagerImpl.java:63)
        at hudson.remoting.CallableDecoratorAdapter.call(CallableDecoratorAdapte
r.java:18)
        at hudson.remoting.CallableDecoratorList$1.call(CallableDecoratorList.ja
va:21)
        at jenkins.util.ContextResettingExecutorService$2.call(ContextResettingE
xecutorService.java:46)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
        at java.lang.Thread.run(Thread.java:745)
    ---- End output of "java" -jar "/var/chef/cache/jenkins-cli.jar" -s http://l
ocalhost:8080 groovy /tmp/groovy20160202-14365-1vd8grq ----
    Ran "java" -jar "/var/chef/cache/jenkins-cli.jar" -s http://localhost:8080 g
roovy /tmp/groovy20160202-14365-1vd8grq returned 255

    Cookbook Trace:
    ---------------
    /var/chef/cache/cookbooks/jenkins/libraries/_executor.rb:84:in `execute!'
    /var/chef/cache/cookbooks/jenkins/libraries/_executor.rb:137:in `groovy!'
    /var/chef/cache/cookbooks/jenkins/libraries/script.rb:48:in `block (2 levels
) in <class:JenkinsScript>'
    /var/chef/cache/cookbooks/jenkins/libraries/script.rb:47:in `block in <class
:JenkinsScript>'

    Resource Declaration:
    ---------------------
    # In /var/chef/cache/cookbooks/jenkins/recipes/plugins.rb

     48: jenkins_script 'sonar' do
     49: command <<-EOH.gsub(/^ {4}/, '')
     50: import jenkins.model.*
     51: import hudson.plugins.sonar.*
     52: import hudson.plugins.sonar.model.*
     53:
     54: def inst =Jenkins.getInstance()
     55:
     56:
     57: def desc =inst.getDescriptor("hudson.plugins.sonar.SonarPublisher")
     58:
     59: def sinst =new SonarInstallation(
     60:   "sonar4.5.1",
     61:   false,
     62:   "http://localhost:9000/",
     63:   "jdbc:mysql://localhost:3306/sonar",
     64:   "com.mysql.jdbc.Driver",
     65:   "sonar",
     66:   "sonar",
     67:   "",
     68:   "",
     69:   new TriggersConfig(),
     70:   "admin",
     71:   "admin"
     72: )
     73: desc.setInstallations(sinst)
     74:
     75: desc.save()
     76: EOH
     77:
     78: end
     79:

    Compiled Resource:
    ------------------
    # Declared in /var/chef/cache/cookbooks/jenkins/recipes/plugins.rb:48:in `fr
om_file'

    jenkins_script("sonar") do
      action [:execute]
      retries 0
      retry_delay 2
      default_guard_interpreter :default
      declared_type :jenkins_script
      cookbook_name "jenkins"
      recipe_name "plugins"
      command "import jenkins.model.*\nimport hudson.plugins.sonar.*\nimport hud
son.plugins.sonar.model.*\n\ndef inst =Jenkins.getInstance()\n\n\ndef desc =inst
.getDescriptor(\"hudson.plugins.sonar.SonarPublisher\")\n\ndef sinst =new SonarI
nstallation(\n  \"sonar4.5.1\",\n  false,\n  \"http://localhost:9000/\",\n  \"jd
bc:mysql://localhost:3306/sonar\",\n  \"com.mysql.jdbc.Driver\",\n  \"sonar\",\n
  \"sonar\",\n  \"\",\n  \"\",\n  new TriggersConfig(),\n  \"admin\",\n  \"admin
\"\n)\ndesc.setInstallations(sinst)\n\ndesc.save()\n"
    end

Victor Martinez

unread,
Feb 2, 2016, 5:46:05 AM2/2/16
to Jenkins Users
You might need to debug your script a bit further as you can see in the STDERROR 

Could not find matching constructor for:
hudson.plugins.sonar.SonarInstallation(java.lang.String, java.lang.Boolean, jav
a.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.S
tring, java.lang.String, java.lang.String, hudson.plugins.sonar.model.TriggersCo
nfig, java.lang.String, java.lang.String)

I believe you are a Sonar plugin version which doesn't support that constructor, you will need to check that, probably it-s worth if you check your sonar version and check the source code too:

My two cents

Anurag Sernapally

unread,
Feb 3, 2016, 1:47:37 AM2/3/16
to Jenkins Users
My plugin version is 2.3 downloaded from https://updates.jenkins-ci.org/download/plugins/sonar/2.3/sonar.hpi and when I check its source code on the link given it doesn't have this library hudson.plugins.sonar*

Victor Martinez

unread,
Feb 3, 2016, 4:17:23 AM2/3/16
to Jenkins Users
No idea what you mean about that library since it does exist otherwise you will get another Exception regarding ClassNotFound and in your case it's an error related to the Constructor itself.

If you debug a bit more you will see the constructor doesn't have the right parameters in your groovy script, see the below source code lines


Cheers

Anurag Sernapally

unread,
Feb 3, 2016, 4:39:31 AM2/3/16
to Jenkins Users
Thanks a lot @Victor, Its fixed.
Reply all
Reply to author
Forward
0 new messages