Continuous integration with Jenkins + Github + Docker + Maven + Junit

At this article I will try to explain some basis of CI with very famous tool Jenkins.

Because I’m lazy I have prepared configured Jenkins with already created job. This tutorial comes originally from here .

So basically wee need only two things

After we had install both SW we only need make two steps

  • create new folder and inside clone following repository
$git clone
  • go to the folder “automate_jenkin” and start the Jenkins by

or if you are on windows

$docker build --no-cache -t jenkins_automate .
$docker run --name docker-jenkins -itd -v /var/run/docker.sock:/var/run/docker.sock -p 1111:8080 -p 50000:50000 jenkins_automate

and that’s it: More detailed instruction how to start Jenkins you can see here.

Now short explanation what we have running.

  • We have Jenkins server (master) which run in docker container
  • On Jenkins Server we have one already created job

go to the http://localhost:1111/jenkins/blue and login with user name and password “admin

click on the job

and run this

as the result you can see this

So you can see we have 3 stages “Build”, “Test”, “Deploy”.
When you click on stage like “Test” you can see all step inside at the left corner.

Definition of every stage and steps in saved in Jenkinsfile in the root of repository that we want to build test deploy,

What the job exactly do?

  • With “agent {docker {” – we are telling we will use docker image
  • The Job pull docker image maven:3-alpine (there is already installed maven and java )
  • mount volume with maven repository inside workspace of Jenkins jobs ( args ‘-v /root/.m2:/root/.m2’ ) Its not necessary but want cache downloaded dependencies by maven
  • We have already cloned repository inside docker, so we can run maven commands
  • install the program into /root/.m2, read NAME and VERSION from pom.xml file and try to run the java program with this
$java -jar target/${NAME}-${VERSION}.jar

Configuration of Jenkins Server

during the start Jenkins server we can set some option, that change setting of Jenkins server.

In this case I created Dockerfile where I set some option for starting Jenkins.

  • set user name and password (by default, the password is generated by jenkins at first)
COPY ./jenkins_config/default-user.groovy /usr/share/jenkins/ref/init.groovy.d/
  • Skip the initial setup wizard
ENV JAVA_OPTS "-Djenkins.install.runSetupWizard=false"
  • change prefix so jenkins will be available on http://localhost:${PORT}/jenkins
ENV JENKINS_OPTS "$JENKINS_OPTS --prefix=/jenkins"

  • switched to user root (default user is jenkins) and installed docker client for connection to docker host
  • add user jenkins into group root

from starting docker container, we can use this arguments

  • exposing port from docker container when will be jenkins available http://localhost:1111/jenkins
$docker run  -p 1111:8080
  • expose docker socket to container
$docker run -v /var/run/docker.sock:/var/run/docker.sock
Docker in Docker

Leave a Reply

Close Menu