Jenkins Servers Fully Loaded

….with your build configuration and build pipeline

Our use case was to create a build pipeline for our Jenkins docker image that we would be deploying to different departments. The obvious choice was to use Jenkins for Jenkins(j4j).

Setting up a build pipeline is easy with Jenkins, but in the true form of build as code, infrastructure as code and our environments being as disposable as possible, I recently tried to get a full running and configured docker image running Jenkins and with build jobs and pipelines ready to be run as soon as the image was deployed.

Turns out it was really easy but I struggled to find clear, concise steps on how to do this so though I would quickly put this article together.

If you have ever installed Jenkins or used the Jenkins docker image, you would have noticed, that you need to view log files to provide the initial password, go through set up screens to install plugins and configure. Using our Dockerfile, you will see how easy it is in the next few lines that we were able to:

  • Define environment variables
  • Install plugins needed
  • Run through the Jenkins default setup page
  • Setup default users and configurations
  • Set up and configure a sample build

The following was how I was able to achieve a fully loaded Jenkins image.

Building the Jenkins Dockerfile

Lets try to keep things as simple and streamlined so for now we are going to use the provided Jenkins image on docker hub. You can find the entire code at the following github repo, but for now we will add the lines for each section:
FROM jenkins:latest

Installing all the plugins you need

If you are using a lightweight docker version of Jenkins, you may need to install script an supporting modules into your image before you can get this running, but by using this you can simply list all of the Jenkins plugins you need just like you would using yum or apt.
# Install plugins
RUN /usr/local/bin/ build-pipeline-plugin docker-plugin git ssh-credentials workflow-aggregator scm-api git-client greenballs docker-build-publish amazon-ecr docker-workflow

Defining Jenkins user and password

Pretty easy using the ENV option in our Dockerfile:

Skipping the initial setup and setting up default users

By setting up another environment variable for the Jenkins application to run from we can bypass the initial setup. The -Djenkins.install.runSetupWizard can be set to false to stop this from running. We can also move our executors and default-users groovy scripts into the /usr/share/jenkins/ref/init.groovy.d/ directory to be used when our docker image is implemented
# Skip initial setup
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false
COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/
COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/

Adding in builds, configuration and other details

The great thing about the ref directory under /usr/share/jenkins/ is that everything in place in the ref directory will be implemented in the Jenkins home directory when the image is launched. This means that any server configurations, build jobs and even build history can be placed into the ref directory and will be in place and ready to role when we start things up.
# Add Build Jobs And Configurations
ADD ref /usr/share/jenkins/ref/

The Jenkinsfile

This is something that Jenkins has put together to make things run a little smoother and allow you to define your build as code. It sit’s nicely in the root of your application repository and defines the build steps for your application in nice groovy format. I won’t go into this in depth as it’s probably best for another time, but at the download you will be able to find a sample.

Install Supporting Applications

Once again, pretty simple where we change into the root user and install all of the supporting applications that we will need to have running in our Jenkins environment.
USER root
RUN apt-get update && apt-get install -y rsync sudo python-setuptools python-dev build-essential && easy_install pip && pip install awscli && rm -rf /var/lib/apt/lists/*

Allow the jenkins user to play as sudo

Not everyone will need this, but there are some circumstances where might want this 
RUN echo “jenkins ALL=NOPASSWD: ALL” >> /etc/sudoers
USER jenkins

Building the Image:
docker build -t jenkins-test .

docker run -d -p 8080:8080 jenkins-test