The Linux Foundation Projects
Skip to main content
Blog

Automating the Build for Docker Images Using Jenkins

This post written by Vedarth Sharma, OMP intern

In the second phase of my internship at Open Mainframe Project (post on first phase here), I automated the build of the ClefOS library of images and also my docker images of SLES15 for s390x. To achieve this, I used Jenkins CI and a little bit of bash scripting.

Setting up the pipeline was the major challenge of my internship. I used Jenkins CI and linked it to the github repository containing all of the source code dockerfiles using a webhook. This webhook sends a POST request to the specified Jenkins server whenever a new commit is pushed. The Jenkins server is hosted on a s390x ClefOS virtual machine on the LinuxONE Community Cloud. The Jenkins server is always ready to receive this POST request. The payload of this request contains details like which repo and which commit triggered the webhook. After parsing the request, Jenkins triggers my pipelines.

One pipeline is for ClefOS and the second is for my s390x SLES15 images. The ClefOS pipeline runs on master node and the SLES15 pipeline runs on a slave node. The pipelines get triggered whenever a new commit (or commits) is/are pushed into master branch of the repo. A single repository contains source code of both ClefOS and SLES15 images https://github.com/openmainframeproject-internship/DockerHub-Development-Stacks.

The pipelines pull the Jenkinsfile from the source code itself. There were just too many ClefOS images such that instructing Jenkins to build each image in Jenkinsfile was getting cumbersome. Instead, each image was assigned a Makefile. This Makefile contained commands to build all of the images from the source code folder, push them and clean the system.

So the initial problem was if we wanted to push the image we had to build it using docker.build() and assign it to a variable for which we can use to call the push() method. The Docker plugin also supports an image method which we can use to trace docker images that have been built before. So I used these two factors to my advantage and wrote a bash script which iterates over all of the folders and executes `make all` in all of them. This script builds all of the images present in ClefOS folder. After that we simply use docker.image() to assign the images to a variable on which we can all the push method. To remove the images, I am using a similar bash script which runs `make clean` command of Makefiles.

This solution has some advantages and some flaws, too. The major advantage is that all the images get built with a single command in Jenkinsfile. But this removes the flexibility of building individual images.

To solve this issue I have also coded the part to build individual images using docker.build(). This code is present in Jenkinsfile as comments. If someone wants to check the build of a single image in the CI they can comment out the execution of build all images bash script and uncomment the code for building an image.

There were also some images which were using hardcoded versions in their source code. I changed that to use the latest available version by scraping a .yml file using a bash script.

The pipelines are scheduled to run on the 22nd of every month. This ensures that images are on the latest version and developers can use them right away and don’t have to deal with outdated images.