CI/CD as Code Part III – Stateless Jenkins Docker Container – Jobs as Code

Stateless Jenkins Container with Docker

The purpose of these sample series is to create a simple set of examples, which showcases CI/CD as Code, using Jenkins. The main goal is to create a stateless Jenkins Docker Container setup, which can be bootstrapped from a set of configuration files, and scripts so that many problems related to maintenance of the infrastructure, and other operational issues are reduced.

 

The previous article of the series summarizes the steps to install and set up Jenkins programmatically. To see how it’s done, please visit CI/CD as Code Part II – Installing and Setting up Jenkins Programmatically

First Steps to “job as code”

Check out this example from here

We are going to introduce the necessary plugins to achieve the goal of programmatic job descriptions in this step and configure them accordingly via programmatic means.

The first plugin that we are going to introduce is the jobDsl plugin. The most common job creation mechanism in Jenkins is that users usually create jobs by cloning/copying an existing project. As experienced by most of us, when the number of jobs grows in Jenkins or the job description gets complicated, the use of the user interface-oriented method becomes more tedious. This is where the jobDsl plugin comes in handy. It provides the programmatic bridge to create jobs using scripts or configuration files.

In order to serve its purpose, the jobDsl plugin uses a freestyle Jenkins job, which is called “Seed Job”. This job is a standard freestyle job to which you add a “Process Job DSLs” step. This step uses the configured DSL and generates jobs configured in it. (For further information please visit the official tutorial)

That’s enough talking. Let’s move to the practical part.

Adding jobDsl support and creating a job using dslScript

We introduced automatic plugin installation support to our stateless Jenkins Docker container instance in the previous step(s). Now, we can use our plugin installation file to add our new plugin.

Open configs/plugins file and addjob-dsl as a new line to this file.

The next step is to configure a seed job by programmatic means. Since we already have the support for “Post-initialization scripts” in our container, we can add a groovy script to create our “Seed Job”. Let’s add a new groovy script file named 1-init-dsl-seed-job.groovy to our init scripts (init.groovy/1-dsl-seed-job.groovy).

Please remember that the reason a numbered prefix is used in the script file names is that these scripts are executed by Jenkins in alphabetical order.

As mentioned earlier, the primary goal of this script is to create the seed job for the plugin. Additionally, we will also add a simple job DSL to the seed job using the
Use the provided DSL script (useScriptText=true)” option. This job, which is going to be created by our seed job, will only print “Hello World!” to the stdout.

Note that, there are more advanced ways to create jobs using the seed job, but simple job definition serves the purpose of this step.

The example job dsl is as follows:

job('example') {
   steps {
       shell('echo Hello World!')
   }
}

The content of the init.groovy/1-dsl-seed-job.groovy script will be as follows:

Test what we did so far

  1. run the docker-compose up –build command against your docker-compose file.
  2. Browse to the Jenkins instance http://localhost:7080 and log in.
  3. Run the SeedJob job.
    The job will fail the first time you run it. It’s because the jobDsl plugin
    limits execution of the scripts without admin approval. In order to approve the execution, go to the configuration page of the job and click save.
  4. A job with the name example must be in place. Check if it exists.
  5. Run the example job and check the output of the job using the console output link on the interface.

Disable Script Security Checks for jobDsl Plugin

If you followed the execution steps in the previous section, you must have noticed the failure of SeedJob and had resolved it manually. Since our goal is to minimize the manual work as much as possible and to get a stateless Jenkins instance, we are going to introduce one additional Jenkins init script to disable script execution limitation for the jobDSL plugin.  The script below does the job for us:

After adding this script to our
init.groovy folder, we can repeat the steps mentioned in the “Test what we did so far” section; however, this time without facing the problem mentioned in item 3 of the list there.