Image for post
Image for post

Jenkins DevKit: Automating Jenkins

Part I: Automating the Jenkins Server

Jenkins is a popular build-task-scheduling tool used to develop continuous integration pipelines.

  • Configuration of Jenkins system
  • Installation of Plugins
  • Creating Jobs and Folder organization
  • Creating Pipelines
  • Creating Triggers for Job or Pipelines
  • Creating Credentials and Using Secrets

Required Tools

You need the following environments:

Creating Jenkins DevKit

In this setup, I show how to get started with crafting our own local Jenkins DevKit using the Docker image jenkins/jenkins.

Step 1: Create Project Structure

mkdir -p ~/projects/jenkins-devkit
cd ~/projects/jenkins-devkit
touch Dockerfile docker-compose.yaml jenkins.yaml

Step 2: Docker Compose script

Add the following content to docker-compose.yaml:

docker-compose.yaml

Step 2: Docker Build Script

Add the following content our initial Dockerfile:

Dockerfile

Step 3: Test Jenkins DevKit

We can bring up our environment by the following commands:

docker-compose up
docker exec -ti jenkins-devkit bash
docker ps

Automating Jenkins DevKit

In this part, we’ll add some automation so any new Jenkins will be configured consistently.

Step 4: Automating Plugin Installation

The Jenkins docker image (jenkins/jenkins) comes with for automation installing plugins at start. All we need to do is supply a list of plugins (plugins.txt) to install-plugins.sh script.

curl -s https://gist.githubusercontent.com/darkn3rd/5da788f00466e2d5d1b3d619710fc647/raw/ > plugins.txt
cat <<-DOCKERFILE >> Dockerfile

####### INSTALL PLUGINS
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt RUN /usr/local/bin/install-plugins.sh \
< /usr/share/jenkins/ref/plugins.txt
DOCKERFILE

Step 5: Automate Bypassing the Setup Wizard

Despite having plugins preinstalled with a new Jenkins server, we will be greeted by a Setup Wizard to install plugins and configure Jenkins. We can bypass this altogether.

cat <<-DOCKERFILE >> Dockerfile####### DISABLE SETUP WIZARD                       
RUN echo $JENKINS_VERSION > \
/usr/share/jenkin/ref/jenkins.install.UpgradeWizard.state; \
echo $JENKINS_VERSION > \
/usr/share/jenkins/ref/jenkins.install.InstallUtil.lastExecVersion
DOCKERFILE

Step 6: Automating Jenkins Configuration

We still need to configure Jenkins with some basic security, such as user accounts. This can be done through a plugin called Jenkins CaSC (or Configuration as Code). This provides a small DSL in YAML to describe the configuration Jenkins and installed Jenkins plug-ins.

jenkins.yaml
cat <<-DOCKERFILE >> Dockerfile####### CONFIGURE JENKINS AND INSTALLED PLUGINS
ENV CASC_JENKINS_CONFIG /var/jenkins_home/casc_configs
RUN mkdir -p /var/jenkins_home/casc_configs
COPY jenkins.yaml /var/jenkins_home/casc_configs/jenkins.yaml
DOCKERFILE
Dockerfile

Step 7: Testing the Final Solution

docker-compose stop
docker-compose build
docker-compose up -d

Testing A Pipeline Manually

We can create a pipeline manually to test the solution. In a followup article, we’ll do this part automatically with automation. Below are two articles that I wrote on how to setup simple build→test pipeline.

Flask (Python) Example

I wrote a previous article on how to create a small hello world web api service, using Flask web microframework:

Sintra (Ruby) Example

I wrote a previous article that is Ruby oriented to create a small hello world web api service using Sinatra web micro-framework:

Clean up

You can remove the container, volume, and image with the following:

# stop and remove running container
docker-compose stop
docker-compose rm
# delete persistent volume
docker volume rm jenkins-devkit_jenkins_data
# purge docker image
docker rmi jenkins-devkit_jenkins:latest

Conclusion

For this article, I wanted to get you started with using a local Jenkins DevKit to that you can start experimenting with automation of Jenkins system, jobs, and pipelines. I will follow up with automating other aspects of Jenkins.

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