Groovy plugin

Plugin Information

Plugin ID groovy
Latest Release 1.13-h-2
Latest Release Date Oct 24, 2013
Sources Github
Support Eclipse Hudson Forum
Issue Tracking Eclipse Bugzilla

This plugin adds the ability to directly execute Groovy code.

Configuration

To configure available Groovy installation on your system, go to Hudson configuration page, find section 'Groovy' and fill the form as shown bellow.


Usage

To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.


The plugin also adds the functionality of the Script Console to the project configuration page. You can schedule your system management script...


...and then observe progress in the build log.


Groovy Script vs System Groovy Script

The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script.

The system groovy script, OTOH, runs inside the Hudson master's JVM. Thus it will have access to all the internal objects of Hudson, so you can use this to alter the state of Hudson.

Changelog

Compatibility Info
The plug-in is being maintained by its owner/maintainers from a new home. Check the Tier info for details of compatibility
Release 1.5 (2010-11-10)
Release 1.4 (2009-12-29)
  • Improve error message for missing groovy executable
  • Update uses of deprecated APIs
Release 1.2
  • Added possibility to specify properties and script parameters.
  • Added script source choice (file/command) for system groovy scripts.
  • Used .exe instead of .bat on Windows (as suggested by Scott Armit).
  • Added configuration option for classpath and initial variable bindings for
    system groovy scripts.

Known bugs

  • Configuring more builders at once actually doesn't absolutely work. If you need more groovy builders in your project, you have to configure them one by one and always save project configuration before you add new one.

Labels:

plugin-builder plugin-builder Delete
tier3-installtest-plugin tier3-installtest-plugin Delete
Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.
  1. Apr 01, 2008

    scott armit says:

    Thanks for this plugin. I've found one issue however (Windows server); if the pa...

    Thanks for this plugin. I've found one issue however (Windows server); if the path to a groovy script passes args, then the full string is quoted. For example, in the "Groovy script file" field, you may have, "build.groovy one two" ('one' and 'two' being args).

    The call to groovy.bat will look like, C:\Groovy-1.5.4\bin\groovy.bat "c:\hudson\jobs\groovy\workspace\build.groovy one two"

    The result is an exception because groovy.bat processes everything in quotes as one long filename:
    Caught: java.io.FileNotFoundException: C:\hudson\jobs\groovy\workspace\build.groovy one two (C:\hudson\jobs\groovy\workspace\build.groovy one two

    There is a hacky workound, after your build script name, put double quotes. In my example, build.groovy" one two . This closes the first double-quote, and the last added one gets ignored.

    Thanks

  2. Apr 01, 2008

    David Vrzalik says:

    Yep, the file field isn't intended for script parameters. Next version should le...

    Yep, the file field isn't intended for script parameters. Next version should let you specify parameters without any hacks.

    1. Apr 02, 2008

      scott armit says:

      Ok, thank you for the information. One other thing I found if you're interested....

      Ok, thank you for the information. One other thing I found if you're interested. Apparently 'groovy.bat' does not handle return codes well. So if you System.exit(1) for instance out of your 'groovy' code, Hudson will still report SUCCESS. It's not Hudson's fault, but groovy.bat. Some notes on the groovy web site mention that using the native launcher fixes this (groovy.exe versus groovy.bat). I downloaded your src and changed ".bat" to ".exe" and built the 'hpi' and it works such that my return codes are not ignored.

      Regards and thanks again for this plugin!

      1. Apr 02, 2008

        David Vrzalik says:

        Thanks, I don't use Windows so I don't have much chance to catch these problems....

        Thanks, I don't use Windows so I don't have much chance to catch these problems. I updated the code to use groovy.exe.

        1. Apr 02, 2008

          scott armit says:

          Wow, thanks for the fast fix. I'm mostly a linux guy, but have to support some W...

          Wow, thanks for the fast fix. I'm mostly a linux guy, but have to support some Windows stuff. Speaking of, I tried out the plugin on linux, and tried my "hack" to pass parameters, but linux being stricter about quotes, the hack doesn't work. So I added a scriptArgs textbox under the scriptFile textbox and it works great. I tested giving the path to a groovy file with spaces in it on windows, and it quoted properly: e.g. <path-to-groovy.exe> "C:\Documents and Settings\user\build.groovy" -option1 -option2

          The code is a little hacky as it's my first attempt at the plugin code, but I can get it to you if you're interested. Thanks again.

  3. Jul 13, 2009

    jvizueta says:

     I want to access a project's parameters from the "This build is parameteri...

     I want to access a project's parameters from the "This build is parameterized" option in the project configuration page, how can I get this done?

     Thank you

    Regards

  4. Sep 10, 2009

    ali raza says:

    How can I configure groovy in hudson. My groovy installation is on a windows. M...

    How can I configure groovy in hudson. My groovy installation is on a windows.

    My install location is
    C:\Program Files\Groovy\Groovy-1.6.4

    I have a SystemVariable in windows called GROOVY_HOME set to C:\Program Files\Groovy\Groovy-1.6.4
    I have added this to the path as %GROOVY_HOME%\bin

    In Hudson manage I am trying to specify the
    groovy installation name as 'Groovy1.6.4'
    GROOVY_HOME as C:\Program Files\Groovy\Groovy-1.6.4

    as soon as I type this I get

    HTTP Status 404 -

    type Status report

    message

    description The requested resource () is not available.

    Apache Tomcat/5.5.27

    Also select 'execute groovy script' does not show the text box where I can specify the groovy script.

    What am I doing wrong.

    Thanks for your help.

  5. Jan 26, 2010

    Phillip Rhodes says:

    Great plugin, thanks!  A couple of questions though... Is running a gro...

    Great plugin, thanks!  A couple of questions though... Is running a groovy script different from running a groovy "system command" in terms of what "stuff" is exposed to the groovy code? That is, the example listed above doesn't work for me when run as a Groovy script.. can I not get access to the hudson internals when running a Groovy script?  What can you access when running Groovy scripts? Do you have visibility into any of the Hudson information about the project, build, etc?  If so, how is it exposed to the Groovy code?

    Thanks in advance for any and all help!

  6. Mar 14, 2010

    Vladimir Drndarski says:

    Phillip Rhodes - If you create a "System Groovy Script" you can access the hudso...

    Phillip Rhodes - If you create a "System Groovy Script" you can access the hudson instance like so:

        Hudson.instance
    

    I'm using it to find all my jobs (other than the one that's executing this script, which has a null SCM) like this:

        branchJobs = Hudson.instance.items.findAll { item -> item.scm.class != NullSCM }
    

    But, I'm having a bear of a time figuring out how to get  groovy access to the environment variables, specifically from the "this build is parameterized" variable that I've defined. 

    If anybody has any clues to how this is done I'd appreciate it.

    //Vlad

    1. Mar 18, 2010

      Christiaan Verbree says:

      EDIT: Ok after reading your comment a second time I don't think that the link w...

      EDIT:

      Ok after reading your comment a second time I don't think that the link will solve the problem you have. It seams to be the same problem I had but I found a workaround yesterday (its not so nice but it is working):

      import hudson.model.*
      
      def job = Hudson.instance.getItem("JobName")
      
      def runs = job.getBuilds()
      def currentBuild = runs[0]
      def vr = currentBuild.getBuildVariableResolver()
      
      println "Current build params are:"
      println "ParamName = " + vr.resolve("ParamName")

      This will get you the values the user entered for the latest build of the given job, if this is the currently running job you will get the parameter for currently running job (tested with hudson 1.351).

      Have a look at http://wiki.hudson-ci.org/display/HUDSON/Display+job+parameters this should do what you want

      greetings
      vkodocha

  7. Mar 18, 2010

    Christiaan Verbree says:

    Is it possible to access the properties set by a parametrized build form within ...

    Is it possible to access the properties set by a parametrized build form within a "Execute System Groovy script" (file or when directly entered in the text box). I can access the properties in an external groovy script ("Execute Groovy script") by going over the System.getenv() path. But when trying the same inside a system one the properties are not set in the environment.

    I tried to add the properties with the "Variable bindings" by specifying testName=$NameOfParameter but this is also not working. Basically the plugin is not resolving $NameOfParameter to the value of this field.

    I think the simplest solution would just be to add the properties from the parametrized build to the environment of the groovy script.

  8. Mar 21, 2010

    Vladimir Drndarski says:

    I finally got my script to fetch the parameterized build value, here's how: &nb...

    I finally got my script to fetch the parameterized build value, here's how:

     

    def job = Hudson.instance.getItem("JobName");   
    def envVars= job.lastBuild.properties.get("envVars");  // this is actually the currently running build, if the script is executed in "JobName"
    println envVars["MyParameterName"];
    

    hope this helps.

    1. Mar 26, 2010

      Michael Rooney says:

      You are my hero, thanks, that works!

      You are my hero, thanks, that works!

  9. Aug 26, 2010

    Fran Simó says:

    Since this script show me user and password on Console Script it doesn't works w...

    Since this script show me user and password on Console Script it doesn't works within a "system groovy script", it show SYSTEM/SYSTEM.
    Any one knows who to know the user's credentials within a task?

    Thanks!

    /* CODE */
    import org.acegisecurity.Authentication;
    import org.acegisecurity.context.SecurityContextHolder;
    println("Usuario: "+(String)hudson.model.User.current());
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String g=auth.getCredentials();
    println("Password: "+ g);
    /* CODE */

    1. Jan 12, 2011

      BARET Didier says:

      I don't anderstand what is this problem. I tried this : println(hudson.model.Us...

      I don't anderstand what is this problem.

      I tried this :
      println(hudson.model.User.current().getFullName());

      it works under the admin script consol but not in an execute system script groovy command : I get SYSTEM and not my user name.

      It's a bug?

  10. Feb 02, 2011

    Mario Shephard says:

    Thanks for this plugin, this seems to be what I need, but I have a question. I ...

    Thanks for this plugin, this seems to be what I need, but I have a question.

    I want to get the current and last builds status, but I'm having trouble getting the current build status.

    All the other things work with that except that build.result which is null. How can I get the status of the build which excecuted this script.

    I'd appriciate any help with this, thanks.

    Here's what I do now.

    def build = Thread.currentThread().executable;
    def projectURL = Hudson.instance.rootUrl + build.project.url;
    def currResult = build.result;
    def prevResult = build.previousBuild?.result ?: null;
    def consecutiveSuccess = currResult == hudson.model.Result.SUCCESS && prevResult == hudson.model.Result.SUCCESS;
    1. Feb 03, 2011

      Mario Shephard says:

      And of course now that I look at it with fresh eyes after well slept night, I no...

      And of course now that I look at it with fresh eyes after well slept night, I notice that of course the build.result is null since the build haven't finished yet.

      So, ignore this question and I apologize for proving the fact that there really is stupid questions.