Jenkins: How to use JUnit plugin when Maven builds occur within Docker container Jenkins: How to use JUnit plugin when Maven builds occur within Docker container jenkins jenkins

Jenkins: How to use JUnit plugin when Maven builds occur within Docker container


You could create an temporary container just before junit to extract test results files to copy test result to your workspsace. And finally remove it

sh 'docker create --name temporary-container spring-image'sh 'docker cp temporary-container:/var/www/java/target/surefire-reports .'sh 'docker rm temporary-container'junit 'surefire-reports'

You could also take a look do docker-pipeline documentations which provides you some abstraction to build docker images


You can use fact that docker container home repository is a shared volume with local jenkins workspace.

Simplest pipeline script:

newApp.inside('-e NODE_ENV=test') { sh 'npm install --dev' sh 'gulp test:unit' } junit 'reports/*.xml'

A bit complex example with some collateral work in separate stages (note that maven build image is using .m2 cache as non-root user):

    try {    buildEnvImg.inside("-v /jenkins/.m2:/var/maven/.m2 -e MAVEN_CONFIG=/var/maven/.m2 -v /jenkins/.cache:/var/maven/.cache -v /var/run/docker.sock:/var/run/docker.sock") {        stage('Compile') {            sh 'mvn -Duser.home=/var/maven -B -DskipTests clean compile'        }        stage('Build jar') {            sh 'mvn -Duser.home=/var/maven -B -DskipTests package'        }        stage('Build docker') {            sh 'mvn -Duser.home=/var/maven -B -DskipTests jib:dockerBuild'        }        stage('Test') {            echo 'Going to execute mvn test, thus automated tests contained in project will be compiled and executed.'            sh 'mvn -Duser.home=/var/maven -B test'        }    }} finally {    junit 'target/surefire-reports/*.xml'}

Solution digged out from Jenkins build inside a docker container with generated reports .