Working with Jenkins Job DSL code sometimes is a pain in the neck. Especially if you have very big and complex scripts. You can make a mistake very quickly, so it would be great if there could be a way to test your code before the seed deployment. Of course, you can test your DSL scripts outside of Jenkins, but sometimes you don’t need to run the whole bunch of tests, right? You just want to check only the small part of your code… What if I tell you that you can do that? Ladies and gentleman, let me introduce you to Jenkins Job DSL Playground!

What is the Jenkins Job DSL Playground?

In short, Jenkins Job DSL Playground is a web application allows you to “transform” your DSL code into the Jenkins XML config. It should be known by every Jenkins Administrator using DSL, it goes without saying! There are several ways of using this application:

official web app at http://job-dsl.herokuapp.com

build your own with the source code

build your own with the docker image

Regardless of which way you will use, the final effect will be the same. You will have access to the simple UI split into the two parts: a place where you can add your code and second column with the execution result. The right column is the place with an XML code – exactly the same as Jenkins XML configuration files. Actually, it’s the only one, certain method to check whether your DSL code does exactly what you want.

How to use Jenkins Job DSL Playground

Everything you find below is based on the official application, available at http://job-dsl.herokuapp.com. But don’t worry – there are no differences between that web app and docker image or the app built from the source code. If you want to use a docker, you can read more about how to build and run the image in my post: Jenkins Job DSL Playground Docker image. But if you would prefer a classic way, just visit the project repository on GitHub and follow the instruction in README file.

TL;DR version

Go to the application in your web browser Type or paste your DSL code into the left column Check if there are no errors during the XML generation. Check if generated XML in the right column matches your expectations.

Longer version

First, you need to open the application in your web browser. You can do that by typing http://job-dsl.herokuapp.com (or http://localhost:5050 or similar if you use your own app). After that you will see the following UI:

Now, paste or type your DSL code to the left column of Jenkins Job DSL Playground. Let me show you an example:

job('Example') { displayName("My example job") steps { shell("echo \"Hello World!\"") } } 1 2 3 4 5 6 job ( 'Example' ) { displayName ( "My example job" ) steps { shell ( "echo \"Hello World!\"" ) } }

As the result, you should see the following XML:

<!-- 1. Example --> <project> <actions></actions> <description></description> <keepDependencies>false</keepDependencies> <properties></properties> <scm class='hudson.scm.NullSCM'></scm> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers></triggers> <concurrentBuild>false</concurrentBuild> <builders> <hudson.tasks.Shell> <command>echo "Hello World!"</command> </hudson.tasks.Shell> </builders> <publishers></publishers> <buildWrappers></buildWrappers> <displayName>My example job</displayName> </project> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!-- 1. Example --> <project> <actions> </actions> <description> </description> <keepDependencies> false </keepDependencies> <properties> </properties> <scm class = 'hudson.scm.NullSCM' > </scm> <canRoam> true </canRoam> <disabled> false </disabled> <blockBuildWhenDownstreamBuilding> false </blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding> false </blockBuildWhenUpstreamBuilding> <triggers> </triggers> <concurrentBuild> false </concurrentBuild> <builders> <hudson.tasks.Shell> <command> echo "Hello World!" </command> </hudson.tasks.Shell> </builders> <publishers> </publishers> <buildWrappers> </buildWrappers> <displayName> My example job </displayName> </project>

And as the image:

As you can see, it’s exactly the same code, as when we create a similar job, but via Jenkins GUI. Thanks to that, we can easily check whether our code matches our expectations.

Jobs troubleshooting with Jenkins Job DSL Playground

But that’s not all! Let’s try to put the following code to our Jenkins Job DSL Playground:

job('Example') { displayName("My example job") parameters { booleanParam("TEST_VARIABLE","true","It should be a boolean param") } steps { shell("echo \"Hello World!\"") } } 1 2 3 4 5 6 7 8 9 job ( 'Example' ) { displayName ( "My example job" ) parameters { booleanParam ( "TEST_VARIABLE" , "true" , "It should be a boolean param" ) } steps { shell ( "echo \"Hello World!\"" ) } }

Do you see what is wrong with that code? No? So please check the output from the right column. In that case, we don’t receive an XML, but an error with the full stacktrace. There you have the most important part:

javaposse.jobdsl.dsl.DslScriptException: (script, line 4) No signature of method: javaposse.jobdsl.dsl.helpers.BuildParametersContext.booleanParam() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [TEST_VARIABLE, true, It should be e boolean param] Possible solutions: booleanParam(java.lang.String, boolean, java.lang.String), booleanParam(java.lang.String), booleanParam(java.lang.String, boolean) 1 2 javaposse .jobdsl .dsl .DslScriptException : ( script , line 4 ) No signature of method : javaposse .jobdsl .dsl .helpers .BuildParametersContext .booleanParam ( ) is applicable for argument types : ( java .lang .String , java .lang .String , java .lang .String ) values : [ TEST_VARIABLE , true , It should be e boolean param ] Possible solutions : booleanParam ( java .lang .String , boolean , java .lang .String ) , booleanParam ( java .lang .String ) , booleanParam ( java .lang .String , boolean )

Don’t you see still what is the problem? We’ve tried to pass the following types of parameters:

(java.lang.String, java.lang.String, java.lang.String)

but we should use: (java.lang.String, boolean, java.lang.String)

We used String instead of Boolean. Yes, Jenkins Job DSL Playground helps us also with such mistakes. Also if we forget to close the brackets, output in the right column will say us about that. Something like this:

job('Example') { displayName("My example job") steps { shell("echo \"Hello World!\"") } 1 2 3 4 5 6 job ( 'Example' ) { displayName ( "My example job" ) steps { shell ( "echo \"Hello World!\"" ) }

will alert us, that we’ve missed the bracket:

javaposse.jobdsl.dsl.DslException: startup failed: script: 6: expecting '}', found '' @ line 6, column 2. } ^ 1 2 3 4 javaposse .jobdsl .dsl .DslException : startup failed : script : 6 : expecting '}' , found '' @ line 6 , column 2. } ^

Easy, right? Easy and very helpful. Just try it on your own and if you have any question, I will try to answer on all of them.