Learning Jenkins Groovy with Jenkins API

2,951 views
Skip to first unread message

Sverre Moe

unread,
Feb 23, 2015, 9:15:50 AM2/23/15
to jenkins...@googlegroups.com
I am trying to learn Groovy and using it in a Jenkins build script.

So far I have run some simple groovy in the script console to Jenkins.
http://jenkins:8080/script

Have tried a simple Groovy script for listing all environment variables, cut'n'paste code from some examples on the web.
Tried to run the groovy script with:
Add build step -> Execute groovy script

Using Groovy Postbuild doesn't work. It will not output a single line of println, neither in parent nor on any axis slave.

I want to access the current build process to get build information and project/job information. Reading the javadoc for Jenkins API is better suited for references and not for learning how to interact with Jenkins. Real example code is hard to find. Much of the example code is also using the deprecated hudson.model instead of jenkins.model.

Using the Jenkins Script console many of the imports are already available. I guess this is not the case when using groovy in build scripts?
I have also tried skimming through the code at https://svn.jenkins-ci.org/trunk/hudson/plugins/ for examples, but that is pure Java code and not Groovy. Though I know pure java code could be used within Groovy.

import hudson.model.*
import jenkins.model.*


println
("TESTING Groovy")


//The current build process:
def thr = Thread.currentThread()
def build = thr?.executable


/**
 * VARIABLES
 */

def config = new HashMap()


def bindings = getBinding()
config
.putAll(bindings.getVariables())


def buildMap = build.getBuildVariables()
config
.putAll(buildMap)


def envVarsMap = build.parent.builds[0].properties.get("envVars")
config
.putAll(envVarsMap)


println
("TESTING Groovy")
config
.keySet().each { key ->
  println
(key + ": " + config.get(key))
}



Sverre Moe

unread,
Feb 23, 2015, 9:46:56 AM2/23/15
to jenkins...@googlegroups.com
Forgot to mention the error message I got while running the Groovy script in my build process:

Caught: groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread
groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread
	at hudson4234154804113492123.run(hudson4234154804113492123.groovy:10)

Rob Mandeville

unread,
Feb 23, 2015, 9:58:43 AM2/23/15
to jenkins...@googlegroups.com, Rob Mandeville

This is definitely a Groovy question, not a Jenkins question.  I’d expect the same thing if you ran it at the command line. But I won’t send you away empty-handed.

 

The doc I’ve seen for Thread is:

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html (java)

http://groovy.codehaus.org/groovy-jdk/java/lang/Thread.html (Groovy)

 

Neither one refers to “executable”.  If you’re trying to find out the executable (likely “java”), I’m not sure how to help you off-hand.  Maybe you can grovel through java.lang.System.properties?

 

--Rob

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fa06fafc-9f6f-4cbb-a0cd-bdfca369bd56%40googlegroups.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.

Sverre Moe

unread,
Feb 23, 2015, 10:19:10 AM2/23/15
to jenkins...@googlegroups.com, rmand...@dekaresearch.com
Found the example on the web. Much of the Groovy examples I have seen entailed using the Jenkins Remote API. I want to use the Jenkins API, running the Groovy script inside jenkins.

Haven't found any example how to get the build process.
def build = jenkins.build (or whatever the access class/method is)

Where is the starting point using the Jenkins API from a groovy script?
Getting server instance, manager instance, thread...

Les Mikesell

unread,
Feb 23, 2015, 3:51:29 PM2/23/15
to jenkinsci-users, rmandeville
On Mon, Feb 23, 2015 at 9:19 AM, Sverre Moe <sverr...@gmail.com> wrote:
> Found the example on the web. Much of the Groovy examples I have seen
> entailed using the Jenkins Remote API. I want to use the Jenkins API,
> running the Groovy script inside jenkins.
>
> Haven't found any example how to get the build process.
> def build = jenkins.build (or whatever the access class/method is)
>
> Where is the starting point using the Jenkins API from a groovy script?
> Getting server instance, manager instance, thread...

Is your build step "execute groovy script" or "execute system groovy
script". The former runs independently in a forked jvm on the node,
the latter is the one with access to jenkins internals.

--
Les Mikesell
lesmi...@gmail.com

Sverre Moe

unread,
Feb 24, 2015, 3:52:17 AM2/24/15
to jenkins...@googlegroups.com, rmand...@dekaresearch.com
I have been using "Execute Groovy Script". Then it looks like I should be using "System Groovy Script" instead. 
Would a groovy script with Scriptler also have access to the Jenkins internals?

Les Mikesell

unread,
Feb 25, 2015, 2:45:46 PM2/25/15
to jenkinsci-users, rmandeville
On Tue, Feb 24, 2015 at 2:52 AM, Sverre Moe <sverr...@gmail.com> wrote:
> I have been using "Execute Groovy Script". Then it looks like I should be
> using "System Groovy Script" instead.
> Would a groovy script with Scriptler also have access to the Jenkins
> internals?
>

I haven't used scriptler so I don't know what control it offers in
terms of running the script. If I were doing it and the script did
anything complex I would probably include it in the project source and
execute the checked out copy so it stays in sync with the version
changes.

--
Les Mikesell
lesmi...@gmail.com
Reply all
Reply to author
Forward
0 new messages