jenkins the definitive guide

(Jeff_L) #1

You can do this in Jenkins with the Execute Shell (for Unix) or Execute Windows Batch command
(for Windows). As an example, in Figure 5.28, “Configuring an Execute Shell step” we have added a
step to execute the Unix ls command.


Figure 5.28. Configuring an Execute Shell step


The output from this build step is shown here:


[workspace] $ /bin/sh -xe /var/folders/.../jenkins2542160238803334344.s
+ ls -al
total 64
drwxr-xr-x 14 johnsmart staff 476 30 Oct 15:21.
drwxr-xr-x 9 johnsmart staff 306 30 Oct 15:21 ..
-rw-r--r--@ 1 johnsmart staff 294 22 Sep 01:40 .checkstyle
-rw-r--r--@ 1 johnsmart staff 651 22 Sep 01:40 .classpath
-rw-r--r--@ 1 johnsmart staff 947 22 Sep 01:40 .project
drwxr-xr-x 5 johnsmart staff 170 22 Sep 01:40 .settings
-rw-r--r--@ 1 johnsmart staff 437 22 Sep 01:40 .springBeans
drwxr-xr-x 9 johnsmart staff 306 30 Oct 15:21 .svn
-rw-r--r--@ 1 johnsmart staff 1228 22 Sep 01:40 build.xml
-rw-r--r--@ 1 johnsmart staff 50 22 Sep 01:40 infinitest.filters
-rw-r--r-- 1 johnsmart staff 6112 30 Oct 15:21 pom.xml
drwxr-xr-x 5 johnsmart staff 170 22 Sep 01:40 src
drwxr-xr-x 3 johnsmart staff 102 22 Sep 01:40 target
drwxr-xr-x 5 johnsmart staff 170 22 Sep 01:40 tools

You can either execute an OS-specific command (e.g., ls), or store a more complicated script as a file
in your version control system, and execute this script. If you are executing a script, you just need to
refer to the name of your script relative to the work directory.


Shell scripts are executed using the -ex option—the commands are printed to the console, as is the
resulting output. If any of the executed commands return a nonzero value, the build will fail.


When Jenkins executes a script, it sets a number of environment variables that you can use within the
script. We discuss these variable in more detail in the next section.


In fact, there are some very good reasons why you should avoid using OS-level scripts in your build jobs
if you can possibly avoid it. In particular, it makes your build job in the best of cases OS-specific, and
at worst dependant on the precise machine configuration. One more portable alternative to executing
OS scripts include writing an equivalent script in a more portable scripting language, such as Groovy
or Gant.

Free download pdf