Image for post
Image for post

Jenkins DevKit: Automating Jenkins

Part 2: Automating Jenkins Jobs and Pipelines

Previously we created a small JenkinsDevKit using Docker-Compose, and then automated installation of plug-ins and a basic configuration.

Previous Article

Highly recommend running through this tutorial that will show how to create initial Jenkins DevKit using Docker Compose, and automates plugin installation and initial server configuration:

Required Tools

You need the following environments:

  • Bash shell (optional): instructions in this guide are oriented toward using Bash.
  • Curl (optional): you may need to download scripts if you skipped the previous article.

Introduction to Pipeline As Code

Jenkins 2.0 introduced Pipelines, where you can define steps in a complex process that allows you…

  • scripted pipeline: imperative groovy script within a node{} block

Declarative Pipeline Example

Below is an example of a declarative pipeline:

  1. Install package manifest using bundler (Gemfile)
  2. Use rake tool to run the tests
  3. Use Junit plugin to present results visually

Scripted Pipeline Example

In the scripted pipeline approach, you will have full control of every step of the code explicitly, which obviously requires more debugging, but has the advantage of giving you greater control.

  1. Build the docker image, which comes with our api server and test code
  2. Run tests within the image
  3. Use Junit plugin to present results visually

Automating Pipeline Jobs

In this section, we’ll create some jobs that use a declarative pipeline (Jenkinsfile) from the remote git repository. These will be a simple pipeline that will run tests on a build agent (which is either a generic ruby or python docker image).

Step 1: Project Home

We should have a current structure under $HOME/jenkins-kit directory that looks like this:

├── Dockerfile
├── docker-compose.yaml
├── jenkins.yaml
└── plugins.txt
cd ~/projects/jenkins-kit
mkdir jobdsl
touch jobs.yaml jobdsl/hello_{flask,ruby}.groovy
├── Dockerfile
├── docker-compose.yaml
├── jenkins.yaml
├── jobdsl
│ ├── hello_flask.groovy
│ └── hello_sinatra.groovy
├── jobs.yaml
└── plugins.txt

Step 2: Existing Files

From the previous article, we should have the following files:

  • Docker build script (Dockerfile)
  • Plugins manifest (plugins.txt)
  • Jenkins Configuration (jenkins.yaml)
# env vars for readability
# download files
curl -s $GIST_PREFIX/$GISTID_PLUGIN_LST/raw/ > plugins.txt
curl -s $GIST_PREFIX/$GISTID_DOCKERFILE/raw/ > Dockerfile
curl -s $GIST_PREFIX/$GISTID_JENKINSCFG/raw/ > jenkins.yaml
curl -s $GIST_PREFIX/$GISTID_DKRCOMPOSE/raw/ > docker-compose.yaml

Step 3: Create Job Configurations

We can use Jenkins CasC to configure jobs in addition to Jenkins server configuration. Jenkins CasC will then use JobDSL to create the jobs.


Step 4: Create Pipeline Jobs

In the previous article, I referenced two repositories for using Sinatra and Flask web microframeworks to do a small hello-world demo API:


Step 4: Update Docker Compose Script

Now that we have our jobs defined, we need to update the docker compose script to support seeding Jenkins with these jobs at startup.

- ./jobs.yaml:/var/jenkins_home/casc_configs/jobs.yaml                             - ./jobdsl:/var/jenkins_home/casc_configs/jobdsl/

Step 5: Test the Solution

Now with everything ready to go, we can bring up our local Jenkins DevKit environment:

docker-compose build
docker-compose up

Running Pipeline Jobs

On the Jenkins server (http://localhost:8080), and you’ll see the testjobs folder with the two jobs we created.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post


Pipelines As Code


This guide’s purpose was to introduce automating the creation of jobs, installing (seeding) jobs at startup, and an introduction to pipelines with Pipeline As Code Jenkinsfile.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store