Provision Jenkins CI | Hands Free

CI Jenkins | Auto Configure with Docker

It pays to automate the entire Jenkins install and configuration. When you are done – manual jenkins job creation will be a thing of the past.

Your developers – at the push of a button, provision a sandbox with Jenkins building and testing their Git code branch – no job creation needed. Push another button to provision a cloud based Jenkins CI environment building your master code.

The CI Toolchain » Jenkins, Nexus, Maven, Git, SpringBoot
Provisioning Toolchain » Docker, Ansible, ec2 (cloud) or Vagrant VirtualBox (local).

Jenkins CI Provisioning Strategy

The plan is straightforward – either use Vagrant (VirtualBox) to create a local VM or use Ansible to spin up cloud based instance. The Jenkins CPU and RAM requirements can make the cloud an expensive and slow place to host Jenkins. Consider local servers and/or 32G RAM intel i9 PCs.

Visit the installing docker on ubuntu article.

Use Dockerfile to Install and Configure Jenkins

The Jenkins Dockerfile starts with an official CloudBees Jenkins container. Before running Jenkins we must

  1. install Maven inside the container
  2. configure the Jenkins admin user (incl name, email, API token)
  3. remove the default Jenkins admin user
  4. copy the necessary Jenkins plugins
  5. copy in the main configuration file
  6. copy the Jenkins job definitions (or Jenkinsfile)

The Dockerfile that gets the above done looks like this.

The Jenkins Container Dockerfile

Here is the Dockerfile that changes the default container into an all-singing all-dancing ready to go continuous integration platform.

Automating Jenkins Job Creation

We use docker to run a jenkins container and then we use the Jenkins REST API to automate job creation. You can create all this in a VM through Vagrant or Ansible or using cloud tools like the Ruby AWS SDK or CloudFormation.

  1. run script to create VM (or server) and install docker
  2. pulls jenkins » sudo docker pull jenkins
  3. runs the container » sudo docker run -d -p 49001:8080 -v $PWD/docker.jenkins:/var/lib/jenkins -t jenkins
  4. remembers first n characters of container
  5. checks the container » sudo docker ps
  6. retrieves logged startup key » sudo docker logs e564fa48dfbd
  7. First jenkins login » http://jenkins-host:49001
  8. Now create a user with name, password, full name and email address
  9. Retrieve API token I » http://jenkins-host:49001/me/configure
  10. Retrieve API token II » click on Show API Token
  11. Enter Api Token as a parameter to the Jenkins Job Creation Script below.

Retrieving Jenkins Startup Key

After running the sudo docker logs e564fa48dfbd command the key will be logged like the below. Use ruby, or python, or bash, or regex to extract the startup key.


Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:


This may also be found at: /var/jenkins_home/secrets/initialAdminPassword


How to Automate Jenkins Job Creation

The tactic we use is to create the job and then use the provided JSON (or XML) to automate the job creation. Save the JSON within your Git or Subversion repository. Use the JSON as a template to create similar jobs.

The steps are

  1. create the job manually and submit it
  2. test that the job (build) works well
  3. click on job’s configure link
  4. scroll to “very” bottom and click on REST API link
  5. click on JSON
  6. save the job’s JSON configuration data
  7. run the Ruby Job creator script (next section)

Now that you have the job’s JSON configuration data you are ready to automate job creation.

The Ruby Jenkins Job Creation Script

The jenkins job creation script needs

  1. the user id
  2. the user api token
  3. the jenkins base url
  4. the job creation JSON (or XML)

Make sure the job is triggered automatically like from a SCM change or when a snapshot dependency is built.

Leave a Reply

Your email address will not be published. Required fields are marked *