An example project demonstrating how you can build spring-petclinic github project using Jenkins pipeline and Docker
Jenkins machine with default plugins installed + Docker installed on the machine - see instructions here
- Login to Jenkins, create a new item based Multibranch Pipeline
- Select as Branch Sources -> GitHub
- Under Repository HTTPS URL, add this github repo url - https://github.com/yaprigal/SpringPetClinic.git
- Add Docker Hub credential under Credential -> Global Credential, set the new credential ID as: dockerhub
The Jenkins pipeline based on docker agent from docker image: yaronpr/mavenwithdocker:3.6.1-alpine
This image is based on maven:3.6.1-alpineimage just with docker client install on it
Why do we need this new docker image ? It's allow us to run docker commands during the Jenkins CI process
How can we connect to docker engine from this image ? By adding the follwing mount:
args '-v /var/run/docker.sock:/var/run/docker.sock'
Download the JCenter configuration file - this setting will be used to resolve dependencies from Bintray JCenter
Clone spring-petclinic project
Execute mvn command by skipping the tests and passing the JCenter settings file
Execute the tests, making sure the step will fail in case of failure in one of the tests and passing the JCenter settings file
In order for our docker image to be layer efficiency(3rd parties will be on seperate docker layer)- we are extracting the resulted JAR
Downloading the Dockerfile (from this repo) and executing the docker build command
Pushing the resulted Docker image to Docker Hub (with small modification, can be also to other Docker registries)
Removing the unused docker image from the build machine in order to save space
- The base image of the Docker image will openjdk:8-jdk-alpine (Docker size consideration)
- We are making sure that we are not running the container with root privileges
- We allowing dynamically to change the port that the application will be listening to by passing APP_PORT during docker build or APP_ENV_PORT during execution of docker run command
- We are also trying to avoid JVM delays by setting java.security.egd=file:/dev/./urandom"