jenkins the definitive guide

(Jeff_L) #1

Now you can add some Groovy scripting to your build job. When you click on “Add build step”, you
will see two new entries in the drop-down menu: “Execute Groovy script” and “Execute system Groovy
script”. The first option is generally what you want—this will simply execute a Groovy script in a
separate JVM, as if you were invoking Groovy from the command line. The second option runs Groovy
commands within Jenkins’s own JVM, with full access to Jenkins’s internals, and is mainly used to
manipulate the Jenkins build jobs or build process itself. This is a more advanced topic that we will
discuss later on in the book.


A Groovy build step can take one of two forms. For simple cases, you can just add a small snippet
of Groovy, as shown in Figure 5.30, “Running Groovy commands as part of a build job”. For more
involved or complicated cases, you would probably write a Groovy script and place it under version
control. Once your script is safely in your SCM, you can run it by selecting the “Groovy script file”
option and providing the path to your script (relative to your build job workspace).


Figure 5.30. Running Groovy commands as part of a build job


In Figure 5.31, “Running Groovy scripts as part of a build job”, you can see a slightly more complicated
example. Here we are running a Groovy script called run-fitness-tests.groovy, which can be
found in the scripts directory. This script takes the test suites to be executed as its parameters—we
provide these in the Script parameters field. If we want to provide any options for Groovy itself, we can
put these in the Groovy Parameters field. Alternatively, we can also provide command-line properties
in the Properties field—this is simply a more convenient way of using the -D command-line option to
pass property values to the Groovy script.

Free download pdf