<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://brokenco.de//feed/by_tag/masochism.xml" rel="self" type="application/atom+xml" /><link href="https://brokenco.de//" rel="alternate" type="text/html" /><updated>2026-10-31T17:58:32+00:00</updated><id>https://brokenco.de//feed/by_tag/masochism.xml</id><title type="html">rtyler</title><subtitle>a moderately technical blog</subtitle><author><name>R. Tyler Croy</name></author><entry><title type="html">Developing Groovy Scripts to Automate Jenkins</title><link href="https://brokenco.de//2017/07/24/groovy-automation-for-jenkins.html" rel="alternate" type="text/html" title="Developing Groovy Scripts to Automate Jenkins" /><published>2017-07-24T00:00:00+00:00</published><updated>2017-07-24T00:00:00+00:00</updated><id>https://brokenco.de//2017/07/24/groovy-automation-for-jenkins</id><content type="html" xml:base="https://brokenco.de//2017/07/24/groovy-automation-for-jenkins.html"><![CDATA[<p>Automation is a wonderful thing, and for the past eight or so years, I have
been a heavy user of [Jenkins](https://jenkins.io() as my hammer of choice for
just about every nail I needed to automate. There’s one dirty little secret
about Jenkins however: it’s a godawful nightmare to try to automate.</p>

<p>My pal <a href="https://github.com/jhoblitt">Josha Hoblitt</a> and I have tried to make
automation of Jenkins configuration slightly less painful for Puppet users with the
<a href="https://github.com/jenkinsci/puppet-jenkins">puppet-jenkins</a> module, but
that’s still rather hackish. To this day, the most effective way to automate
configuration in Jenkins is to use its built-in
<a href="https://groovy-lang.org">Groovy</a> scripting support and the little-known
feature of loading scripts from the <code class="language-plaintext highlighter-rouge">init.groovy.d/</code> directory.</p>

<p>The <code class="language-plaintext highlighter-rouge">init.groovy.d/</code> directory is an optional directory you can create in
<code class="language-plaintext highlighter-rouge">JENKINS_HOME</code>, typically <code class="language-plaintext highlighter-rouge">/var/lib/jenkins</code>. The directory can be filled with
Groovy scripts which Jenkins will run when it starts, <em>after</em> it has loaded
plugins, but <em>before</em> it starts servicing user requests and scheduled
workloads. The Groovy scripts in this directory are effectively the same as one
might execute in the <a href="https://jenkins.io/doc/book/managing/script-console/">Script
Console</a> or via the
<code class="language-plaintext highlighter-rouge">groovy</code> <a href="https://jenkins.io/doc/book/managing/cli/">CLI command</a>.</p>

<p><img src="/images/post-images/groovy-jenkins/script-console.png" alt="Script Console" /></p>

<p>“Cool!” I’m sure you’re thinking, but slow down there buckaroo. Developing
these Groovy scripts, as luck would have it, is <strong>painful</strong>. The Groovy scripts
manipulate internal state of the Jenkins instance, by constructing objects and
executing methods on Jenkins’ own object graph.</p>

<p>Here’s a simple example which sets the number of executors on the master to
zero:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/*
 * Make sure the number of executors available on the master is set to zero
 */

import jenkins.model.*
Jenkins.instance.setNumExecutors(0)
</code></pre></div></div>

<hr />

<h3 id="detour">Detour!</h3>

<p>Experienced Jenkins users and administrators might be familiar with all the
<code class="language-plaintext highlighter-rouge">.xml</code> files which litter <code class="language-plaintext highlighter-rouge">JENKINS_HOME</code>. “XML, that’s used for uglier versions
of JSON documents right?” In most other cases, this is probably correct, but
not in Jenkins. In Jenkins, for the most part, these <code class="language-plaintext highlighter-rouge">.xml</code> files are
<a href="https://en.wikipedia.org/wiki/XStream">XStream</a> serializations of objects; they
are <strong>not</strong> typical XML documents.
Some slices of the object graph in Jenkins is serialized and written to disk.
It’s kind of like Smalltalk, but with the JVM and XML.</p>

<hr />

<h3 id="developing-groovy-for-jenkins">Developing Groovy for Jenkins</h3>

<p>So these Groovy scripts must manipulate the object graph in Jenkins in order to
accomplish anything, meaning that the poor sap who tries to automate Jenkins
must understand Jenkins internal APIs in order to be successful! Yay! Here are
some tips help:</p>

<ol>
  <li>Use the Docker image to quickly iterate.</li>
  <li>Use the web UI to configure settings visually, then compare the XML</li>
  <li>Rely on <a href="http://javadoc.jenkins.io">javadoc.jenkins.io</a> and <a href="http://javadoc.jenkins.io/plugin/">plugin javadocs</a> to get call invocations correct.</li>
  <li>Verify in the Script Console</li>
</ol>

<h4 id="iterating-with-docker">Iterating with Docker</h4>

<p>In an example source tree, imagine I have <code class="language-plaintext highlighter-rouge">init.groovy.d/</code> with my Groovy
scripts, I rely heavily on the <a href="https://github.com/jenkinsci/docker">Docker
image</a> and will typically use a command
like the following to run a local instance of Jenkins with my Groovy scripts:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>docker run --rm -ti -p 8080:8080 \
    -v $PWD/init.groovy.d:/var/jenkins_home/init.groovy.d \
    -v $PWD/jenkins-tmp:/var/jenkins_home \
    -e JAVA_OPTS=-Djenkins.install.runSetupWizard=false \
    jenkins/jenkins:lts-alpine
</code></pre></div></div>

<p>This will map my current directory’s <code class="language-plaintext highlighter-rouge">init.groovy.d</code> into the approprate path
inside the container. I also map in <code class="language-plaintext highlighter-rouge">jenkins-tmp</code> so I can inspect <code class="language-plaintext highlighter-rouge">.xml</code> files
after runnign Jenkins, and finally I disable the new installation “Setup
Wizard” making iteration easy.</p>

<p>Whenever I’m satisfied that my scripts are doing the right thing, I’ll stop
the container, and restart it so it loads my script like any other “pristine” Jenkins.</p>

<h4 id="using-the-web-ui-to-generate-xml">Using the web UI to generate XML</h4>

<p>Think of the Jenkins web UI as a pretty way of interacting with the gnarly
object graph underneath. By making changes in the web UI, and then inspecting
<code class="language-plaintext highlighter-rouge">.xml</code> files for the changes. Finding the right <code class="language-plaintext highlighter-rouge">.xml</code> file matching the right
web UI settings requires a little bit of guess and check, but here are some
rough pointers</p>

<ul>
  <li>Most basic settings from “Configure System” live in <code class="language-plaintext highlighter-rouge">config.xml</code></li>
  <li>Global settings provided by plugins might be found in files which start with
<code class="language-plaintext highlighter-rouge">org.jenkinsci.plugins.</code> or <code class="language-plaintext highlighter-rouge">hudson.plugins.</code>, e.g. <code class="language-plaintext highlighter-rouge">hudson.plugins.git.GitSCM.xml</code>.</li>
</ul>

<p>If I look at the file <code class="language-plaintext highlighter-rouge">hudson.plugins.git.GitSCM.xml</code> after configuring some
Git specific settings, it contains:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;hudson.plugins.git.GitSCM_-DescriptorImpl plugin="git@3.3.2"&gt;
    &lt;generation&gt;1&lt;/generation&gt;
    &lt;globalConfigName&gt;tyler&lt;/globalConfigName&gt;
    &lt;globalConfigEmail&gt;tyler@example.com&lt;/globalConfigEmail&gt;
    &lt;createAccountBasedOnEmail&gt;false&lt;/createAccountBasedOnEmail&gt;
&lt;/hudson.plugins.git.GitSCM_-DescriptorImpl&gt;
</code></pre></div></div>

<p>Which gives me clues to which Javadocs to look at for API signatures in my
Groovy scripting. I’ll be looking for a class named <code class="language-plaintext highlighter-rouge">GitSCM</code> and its
constructor agruments.</p>

<h4 id="referencing-javadoc">Referencing Javadoc</h4>

<p>In the example above, I would refer to the <a href="http://javadoc.jenkins.io/plugin/git">Git plugin’s
Javadoc</a> and start digging around in
there. This is probably the most challenging stage of developing Groovy
automation for Jenkins, figuring out which classes need to be constructed and
which methods might need to be called. A <strong>lot</strong> of experimentation is needed
here, which is where the Script Console comes in.</p>

<h4 id="prototyping-in-the-script-console">Prototyping in the Script Console</h4>

<p>In my Docker image-based environment, I can make breaking changes in the Script
Console without much consequence. As I develop a <code class="language-plaintext highlighter-rouge">.groovy</code> file, I’ll paste
snippets in the Script Console and click <strong>Run</strong>, verifying that the script is
doing what I expect it to do, using the web UI to cross-reference.</p>

<p>Once my little bit of groovy is working as expected, I’ll commit it and move
onto the next item.</p>

<hr />

<p>Automating Jenkins isn’t <em>easy</em> but if you know where to look, it doesn’t need
to be impossible either. The nice thing about the Groovy approach, as I have
used it in the past, is <code class="language-plaintext highlighter-rouge">init.groovy.d/</code> can run at the instance’s boot time,
and then I can use the same file, run it through the <code class="language-plaintext highlighter-rouge">groovy</code> CLI command and
ensure that the instance’s settings are correct.</p>

<p>I hope you have found this brief reprieve from <a href="/tag/garden.html">gardening blog
posts</a> useful!</p>]]></content><author><name>R. Tyler Croy</name></author><category term="jenkins" /><category term="groovy" /><category term="masochism" /><summary type="html"><![CDATA[Automation is a wonderful thing, and for the past eight or so years, I have been a heavy user of [Jenkins](https://jenkins.io() as my hammer of choice for just about every nail I needed to automate. There’s one dirty little secret about Jenkins however: it’s a godawful nightmare to try to automate.]]></summary></entry></feed>