Over a million developers have joined DZone.

The Jenkins Job DSL Plugin in Practice

How the DSL plugin for Jenkins allows users to describe jobs using a Groovy-based language and manage the scripts and updates of Jenkins jobs.

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Not that long ago, one of our engineers made a mistake while running a Jenkins CLI script and removed all jobs apart from the ones he wanted to remove. Looking at how many jobs we have at the moment, it might have been over 300 jobs combined into dozens of pipelines. Pretty scary when you consider that we were not running scheduled backups. Well, in my opinion it was the best test for the continuous delivery platform we were working on for a couple of weeks. In less than 15 minutes we had all the jobs back and it took so long because we neither wanted to skip tests during the restoration nor bring back unneeded jobs. How is that possible? Let me introduce Jenkins Job DSL / Plugin, a project made up of two parts: the Domain Specific Language that allows users to describe jobs using Groovy-based language, and a Jenkins plugin which manages the scripts and the updating of Jenkins jobs which are created and maintained as a result.

But before we jump into job-dsl lets take a step back and take a brief look at how Jenkins jobs are configured. If you create a new job and go to http://<JENKINS_HOST>/job/<JOB_NAME>/config.xml you will get the following XML:

<project>
  <actions/>
  <description/>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>
</project>

Not that interesting, so let’s add Execute shell build step that prints Hello world!. Immediately you will see changes to the builders node in config.xml:

<builders>
  <hudson.tasks.Shell>
    <command>echo 'Hello world!'</command>
  </hudson.tasks.Shell>
</builders>

If you want to view changes to XML on each change made, install JobConfigHistory plugin.

Still nothing spectacular, so let’s parameterize the build and look at the changes to config.xml where properties node should now contain parameter definition:

<properties>
  <hudson.model.ParametersDefinitionProperty>
    <parameterDefinitions>
      <hudson.model.StringParameterDefinition>
        <name>MESSAGE</name>
        <description/>
        <defaultValue>Hello world!</defaultValue>
      </hudson.model.StringParameterDefinition>
    </parameterDefinitions>
  </hudson.model.ParametersDefinitionProperty>
</properties>

The Execute shell build step should reflect that we’re using this parameter in echo command:

<builders>
  <hudson.tasks.Shell>
    <command>echo $MESSAGE</command>
  </hudson.tasks.Shell>
</builders>

Even though I haven’t told what steps I had taken you shouldn’t have trouble replaying them — those XMLs are descriptive enough (as for XML).

Ok, it’s getting boring so let’s do something more sophisticated and install Rebuild plugin to help us re-run parameterized jobs. Installing this plugin should yield no changes to existing jobs (in the end they run on plugin defaults) but when you create a new job you should find the Rebuild plugin configuration under properties node:

<properties>
  <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.25">
    <autoRebuild>false</autoRebuild>
    <rebuildDisabled>false</rebuildDisabled>
  </com.sonyericsson.rebuild.RebuildSettings>
</properties>

Now after we enable Rebuild Without Asking For Parameters for our existing job, it can be re-run with previous parameters without having to go through a view listing all the parameters as reflected in the com.sonyericsson.rebuild.RebuildSettings node:

<com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.25">
  <autoRebuild>true</autoRebuild>
  <rebuildDisabled>false</rebuildDisabled>
</com.sonyericsson.rebuild.RebuildSettings>

Still boring as XML (pun intended). Fortunately, Groovy makes working with XML much more pleasant and has fantastic capabilities for writing DSLs. This, along with Jenkins' built-in Groovy support (it features a Groovy script console that I already mentioned in this post), makes a perfect match for job-dsl plugin that we’re going to explore now.

Let’s start with the DSL and try to get the equivalent of config.xml with Groovy-based DSL. Armed with API docs (should you feel lost start from buildFlowJob node) take a moment and experiment on Job DSL Playground. You should end up with a script similar to this one:

job('parameterized-hello-world') {
   parameters {
     stringParam('MESSAGE', 'Hello world!') 
   }
   properties {
     rebuild {
       autoRebuild()
     }
   }
  steps {
    shell('echo $MESSAGE')
  }
}

Way more friendly than XML, and it’s code that you can put under version control. Moreover you don’t have to click through a GUI to get this configuration. But how do you run it in Jenkins? Well, it’s as simple as creating (let’s do it manually for now) and running a seed job which, is a regular job with a Process Job DSLs build step containing your script (or even better, pointing to the file containing the script). In no time you should see a successful build informing you that it has generated a new job:alt textAnd the generated job will hold information that it has been generated by the seed job:alt text

And if you ever want to change anything about the generated job you just apply changes to the DSL script (that you keep under version control) and re-run the seed job (that points to the script(s) under version control). Like an old record, I will once again mention version control. Please keep your scripts under version control!

Now you can revoke all permissions for manual job configuration and make all changes via code!

Should job-dsl lack support for your favourite plugin just submit a pull request. It should be easy after getting the basic concepts and if you need something to start with you can take a look how we’ve added support for Rebuild plugin.

After you get familiar with the DSL you can build your own around it to further simplify jobs’ (or even whole pipelines) configuration. You can also consider describing pipelines as metadata kept inside projects’ repositories that are scanned by a periodically run seed job that parses that metadata and (re-)generates jobs on any change. Since it’s code, you can craft anything your CI/CD solution needs.

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:
jenkins ,continuous delivery ,continuous integration

Published at DZone with permission of Kamil Szymański. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}