In this Video

OpenShift provides official Jenkins images that run in containers on OpenShift.

Jenkins is a widely popular and open-source continuous integration and continuous delivery (CI/CD) tool that is used to automate building, testing and deploying application projects. OpenShift provides an official Jenkins image that runs in containers on OpenShift with the OpenShift Pipeline Plugin pre-installed which allows performing actions in OpenShift within Jenkins jobs.

OpenShift 3.2, introduced Jenkins Source-to-Image (S2I) feature which simplifies customization of the official Jenkins image through the S2I build process. You can use Jenkins S2I to copy your custom Jenkins job definitions, additional plugins, or replace the provided config.xml file with your own custom configuration. The resulting image is stored in the OpenShift registry and can be used to deploy pre-configured Jenkins instances.

The Kubernetes Plugin is also pre-installed in the official Jenkins image. This plugins allows Jenkins master to create slave pods on OpenShift and delegate running jobs to them to achieve scalability as well as providing pods with specific runtimes for specific jobs.

This video demonstrates how to use Jenkins S2I and run distributed builds through the Kubernetes Plugin.

Setting Up Jenkins

An example template for using the Jenkins S2I builder as well as creating custom slave images for building Java apps is available on GitHub:

https://github.com/siamaksade/jenkins-s2i-example

Create a new project for Jenkins:

$ oc new-project ci --display-name="CI/CD"

Give the Jenkins pod service account enough rights to be able to make API calls to OpenShift. This allows Jenkins to automatically discover slave images available in the registry.

$ oc policy add-role-to-user edit -z default -n ci

The Jenkins slave template takes any image as an input and layers Jenkins JNLP client on top of it to be used as a Jenkins slave pod. The default parameters turn a RHEL 7 image to a Jenkins slave pod that can run JDK 8 jobs. Import the Jenkins master and slave template:

$ oc create -f https://raw.githubusercontent.com/siamaksade/jenkins-s2i-example/master/jenkins-master-s2i-template.yaml
$ oc create -f https://raw.githubusercontent.com/siamaksade/jenkins-s2i-example/master/jenkins-slave-builder-template.yaml

Create a Jenkins slave image using the Jenkins slave template. You can repeat this step for all slave image types (ruby, python, etc) you need. Image Name is the name of the image you want to convert to a Jenkins slave image and Jenkins Slave Label is the slave label used by Jenkins master to discover slave pods and delegate job to them.

image00

The template defines a build which can be viewed in Browse → Builds section. After build is finished, create the Jenkins master based on the master template via OpenShift Console or CLI:

$ oc new-app jenkins-master-s2i

Running Distributed Builds

Point your browser to Jenkins url as displayed in OpenShift Console and login with default admin user:

Username: admin

Password: password

A build job is already defined by Jenkins S2I based on the job configuration which was provided in the GitHub repo. Click on tasks-build job and then Configure. Check “Restrict where this project can be run” and specify jdk8 for Label Expression.

image0444

Note that Label Expression is how Jenkins finds the slave pods and assigns jobs to them. If you have chosen a different label when instantiating the Jenkins slave template, use the same value in the job configuration.

Start the tasks-build job and note that a slave pod gets created on-demand in OpenShift, runs the jobs and gets removed afterwards.

Conclusion

Jenkins S2I builder simplifies creating custom Jenkins images based on official Jenkins image in OpenShift by providing plugins, job and other configurations in a Git repository. This approach also promotes the infrastructure-as-code principle by applying version control on Jenkins master configurations.
Furthermore, using the Kubernetes Plugin allows running distributed builds at scale and creating Jenkins slave pods on-demand on OpenShift when running Jenkins jobs.