Public | Automated Build

Last pushed: a month ago
Short Description
Jenkins with the DSL plugin ready to create your jobs
Full Description

Jenkins DSL ready



Goal: automate your Jenkins installation. Get Jenkins and its jobs ready with one docker command!

This Jenkins image is based on top of the official Jenkins image and as such provides all its features.
Additionally, it comes with the Job DSL plugin ready to use.

tl;dr

docker run -d -p 8080:8080 tomdesinto/jenkins-dsl-ready

docker run -d -p 8080:8080 -v /your/dsl/files/:/usr/share/jenkins/ref/jobs/SeedJob/workspace/:ro tomdesinto/jenkins-dsl-ready

docker run -d -p 8080:8080 -e SEEDJOB_GIT=https://your.repo.git tomdesinto/jenkins-dsl-ready

docker run -d -p 8080:8080 -e SEEDJOB_SVN=svn://your.repo tomdesinto/jenkins-dsl-ready

What does it do?

When you start the container the following happens:

  1. All elements (plugins, jobs, config, etc) from /usr/share/jenkins/ref/ which are not yet in $JENKINS_HOME are copied over
  2. Jenkins starts
  3. all Groovy scripts found in $JENKINS_HOME/init.groovy.d/ are run, which includes our create-seed-job.groovy script
  4. The SeedJob is eventually created and a run is scheduled if it was missing
  5. The SeedJob, if run, creates additional jobs found in its workspace:
    • by default the groovy scripts are provided by the docker image (see the dsl/ directory content)
    • if a git repository url is provided with the SEEDJOB_GIT environment variable, the SeedJob will fetch the groovy scripts from there
    • else if a svn repository url is provided with the SEEDJOB_SVN environment variable, the SeedJob will fetch the groovy scripts from there

Included plugins

Usage

Default DSL jobs

By running the image as follow:

docker run -d -p 8080:8080 tomdesinto/jenkins-dsl-ready

You will end up with an instance of Jenkins that demonstrates the usage of the DSL plugin. In this configuration you will have 2 default jobs created additionally to the SeedJob.

From there you can edit the SeedJob and make it fetch your DSL scripts from a SVN/git repository and make it create your other jobs.

Providing DSL jobs with Git/SVN

If you want to provide the DSL scripts from a remote repository, use either the SEEDJOB_GIT or SEEDJOB_SVN environment variables.

docker run -d -p 8080:8080 -e SEEDJOB_GIT=https://your.repo.git tomdesinto/jenkins-dsl-ready
docker run -d -p 8080:8080 -e SEEDJOB_SVN=svn://your.repo tomdesinto/jenkins-dsl-ready

Providing DSL jobs from a directory

You can provide you groovy DSL files from a directory on your docker host by mounting this directory with a docker volume on /usr/share/jenkins/ref/jobs/SeedJob/workspace/.

docker run -d -p 8080:8080 -v /somewhere/on/your/host/dsl/:/usr/share/jenkins/ref/jobs/SeedJob/workspace/:ro tomdesinto/jenkins-dsl-ready

Using Docker within jobs

Method 1 - Sharing the jenkins-dsl-ready Docker Host engine (root)

If you want your jobs to be able to make use of the Docker engine running on the Jenkins container host, first note that this is insecure as any Jenkins jobs will be able to take control of the full Docker engine (meaning even deleting any image/container on the Docker host), then you need to start the jenkins-dsl-ready container a bit differently:

  • Jenkins must be run by the user root
  • The /var/run/docker.sock socket file must be mounted as a volume

In the end, the command to run such a container is:

docker run -d \
    -u root \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):/usr/bin/docker:ro \
    -p 8080:8080 \
    --name jenkins \
    tomdesinto/jenkins-dsl-ready

From now on, you can call directly the docker command.

Method 2 - Sharing the jenkins-dsl-ready Docker Host engine (sudo)

Same as method 2, but we don't run Jenkins as root. In this case the Jenkins jobs will have to use sudo docker instead of just docker:

docker run -d \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):/usr/bin/docker:ro \
    -p 8080:8080 \
    --name jenkins \
    tomdesinto/jenkins-dsl-ready

In this setup, you can use docker with sudo: sudo docker.

Care should be given to files rights in Jenkins jobs. If a job makes use of sudo to run a command which will write files in the job workspace, those files
will be owned by root. Jenkins would then be unable to manage then (wipe workspace, clear, etc) unless your job also makes sure to call chown jenkins on them.

Method 3 - Using dind (Docker in Docker)

Using the official docker:dind image, you can start a container which runs another child Docker engine which will be available to your jenkins-dsl-ready container through links. Be aware of constraints and pitfalls that comes with such a setup. Make sure to read Using Docker-in-Docker for your CI or testing environment? Think twice from Jérôme Petazzoni.

docker run -d --privileged --name dind docker:1.8-dind

note: use the tag that matches your docker version. i.e.: docker:1.8-dind if you have docker v1.8.1 or v1.8.2. docker:1.7-dind if you have docker v1.7.x, and so on. See available tags at https://hub.docker.com/r/library/docker/tags/

You would then start the jenkins-dsl-ready container with:

docker run -d \
    -v $(which docker):/usr/bin/docker:ro \
    -p 8080:8080 \
    --link dind:dind \
    -e DOCKER_HOST=tcp://dind:2375 \
    --name jenkins \
    tomdesinto/jenkins-dsl-ready

From now on, you can call directly the docker command within Jenkins jobs.

Troubleshooting

If docker fails with error Error response from daemon: client is newer than server (client API version: 1.20, server API version: 1.19), or similar, then
it means the version of the Docker client from the jenkins-dsl-ready image is newer than the Docker engine from the dind image. Refer to the note above to start a dind container having the right version of docker.

If docker fails with error docker: error while loading shared libraries: libapparmor.so.1: cannot open shared object file: No such file or directory, then you need to mount another volume on your container to enable the docker process to use the appArmor shared libraries. Depending on your system, the exact location of the librairies might differ from the following example: -v /usr/lib/x86_64-linux-gnu/libapparmor.so.1.1.0:/usr/lib/x86_64-linux-gnu/libapparmor.so.1. More info at https://github.com/SvenDowideit/dockerfiles/issues/17.

DSL syntax

Refer to the DSL Job reference. If you are just discovering the DSL
Plugin, you should start with the tutorial.

Example of DSL script

Customizing the image

Other default DSL files

You can add default DSL scripts to the dsl/ directory. When you build the docker image, those scripts will be copied to the SeedJob workspace when the container will be run.

Add software

Your jobs might depend on software which is not available in this image. You can build your own image with additional software by adding the commands to install them after the customize below section.

###############################################################################
##                          customize below                                  ##
###############################################################################

# Eventually place here any `apt-get install` command to add tools to the image
#


# COPY your Seed Job DSL script
COPY dsl/*.groovy /usr/share/jenkins/ref/jobs/SeedJob/workspace/
Docker Pull Command
Owner
tomdesinto

Comments (0)