Jenkins build inside a docker container with generated reports
Asked Answered
E

2

7

I am new to Jenkins and Docker and even after some researches, I do not find the way to do these things.

I want to :

  • Execute pytest and python-coverage on my project inside a docker container.
  • This should generate test and coverage reports
  • Access the generated reports and read them with some Jenkins'plugin.

When I try locally with Docker, it is working. I created a Dockerfile which creates a docker image with the libs needed and the source code inside it, then a script is called when the container is created and run the tests. I can see that it is working because I used cat and was able to see the generated reports inside my terminal.

My problem here is : How can I have access to the reports generated inside the container in Jenkins and read them with plugins after.

EDIT : So here an example of what I am trying to do so you can have a better idea.

First, my Dockerfile example :

# starting from debian image
FROM debian

# install pytest and coverage to execute my tests
RUN apt-get update && apt-get install -y \
    python-pytest \
    python-coverage

# add source files to the image
ADD . /HelloPython/

WORKDIR /HelloPython/

# execute shell script which run tests
CMD sh ./compil.sh

My compil.sh contains my tests execution

# Run unit tests and generate JUnit reports in the reports directory
py.test --junitxml reports/test-results.xml test*.py

# Generate reports of the test code coverage
python-coverage run -m unittest discover
python-coverage xml -o reports/test-coverage.xml

And here my jenkins log when I run it with the Cloudbees plugin :

Démarré par l'utilisateur chris
Building in workspace /var/lib/jenkins/workspace/HelloPythonCover
Build Docker image from ./Dockerfile ...
$ docker build --file /var/lib/jenkins/workspace/HelloPythonCover/Dockerfile /var/lib/jenkins/workspace/HelloPythonCover
Sending build context to Docker daemon 8.704 kB

Step 1 : FROM debian
 ---> 1b088884749b
Step 2 : RUN apt-get update && apt-get install -y   python-pytest   python-coverage
 ---> Using cache
 ---> a5883bbc27e4
Step 3 : ADD . /HelloPython/
 ---> c03ecb80040c
Removing intermediate container d2cc8ea14c11
Step 4 : WORKDIR /HelloPython/
 ---> Running in dc3b08c6fa02
 ---> 20f41970849c
Removing intermediate container dc3b08c6fa02
Step 5 : CMD sh ./compil.sh
 ---> Running in 14ceca0e5975
 ---> 853cb296b94f
Removing intermediate container 14ceca0e5975
Successfully built 853cb296b94f
Docker container faaedb777e032e38586278ad776e1561a9f1c5a92536c06bca7e3af12b74a355 started to host the build
$ docker exec --tty faaedb777e032e38586278ad776e1561a9f1c5a92536c06bca7e3af12b74a355 env
[HelloPythonCover] $ docker exec --tty --user 116:125 faaedb777e032e38586278ad776e1561a9f1c5a92536c06bca7e3af12b74a355 env BUILD_DISPLAY_NAME=#29 BUILD_ID=29 BUILD_NUMBER=29 BUILD_TAG=jenkins-HelloPythonCover-29 BUILD_URL=http://localhost:8080/job/HelloPythonCover/29/ CLASSPATH= EXECUTOR_NUMBER=0 HOME=/root HOSTNAME=faaedb777e03 HUDSON_HOME=/var/lib/jenkins HUDSON_SERVER_COOKIE=bd683ee6091ff880 HUDSON_URL=http://localhost:8080/ JENKINS_SERVER_COOKIE=bd683ee6091ff880 JENKINS_URL=http://localhost:8080/ JOB_NAME=HelloPythonCover JOB_URL=http://localhost:8080/job/HelloPythonCover/ NODE_LABELS=master NODE_NAME=master PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin TERM=xterm WORKSPACE=/var/lib/jenkins/workspace/HelloPythonCover /bin/sh -xe /tmp/hudson6836918802627685893.sh
Stopping Docker container after build completion
Finished: SUCCESS

So my primary goal here is to find a way for jenkins to get access to the reports generated. Also, Is there a way to see what is going inside the docker container while building ? For example, I tried to put a cat inside my shell script to see the reports while I tried locally, but in Jenkins I can not find a way to see it.

Emmett answered 20/7, 2016 at 9:1 Comment(6)
which build mechanism you are using? could you checked the log location mentioned in your build file?Tubby
My explanation is wrong, I will edit it. In fact, I am just running pytest and python-coverage on my source code to generate reports about unit tests and code coverage.Emmett
I suggest shared volumes.Foothill
Do you use this plugin?Dull
I use this one. I do not really know which would be better for my case.Emmett
What's your host OS? Did you read all the documentation on the plugin? Seems a common use-case.Uro
D
2

Either Docker Plugin or Docker Slaves Plugin should work fine.

Just be sure that the generated report is in xUnit format that Jenkins can "understand" and add a Post-build Action to publish the tests result pointing to the right path where it is like this:

Publish JUnit Test

Jenkins will copy that generated report to outside the container.

If your coverage tool is generating HTML reports you can use the HTML Publisher Plugin just like the publish step described above.

Delectation answered 20/7, 2016 at 14:44 Comment(6)
The generated reports are in JUnit format so it is okay. However, I tried with the docker plugin but at the beginning of the build, I try to execute my tests with "py.test --junitxml reports/test-results.xml test*.py" but it says that it can not find the file. I tried with and without my sources in the docker image, but it is not working for both.Emmett
There could be a problem in your tests or at least in the test reporter. To make sure your tests are generating a report, try running a Shell script listing the files inside that destination path you defined, do this as build step right before the post actions section for debugging.Firmament
My tests seem to work properly. When I execute it in local, or in jenkins without being in a container, everything is working great. I think the problem is that i have not access to what is inside the container. The "only" thing I need to do is having access to the files inside the container, and I did not find how to do it at this time.Emmett
@Emmett did you manage to get this working inside a container?Buffer
@Buffer If I remember correctly, I managed to get this working some weeks later but I don't remember how, sorryEmmett
@whin3 Ok... no problem for me i managed this working using pipeline script ` newApp.inside('-e NODE_ENV=test') { sh 'npm install --dev' sh 'gulp test:unit' } junit 'reports/*.xml'` As docker container home repository is a shared volume with local jenkins workspaceBuffer
F
2

Docker copy (docker cp) can copy files from the container file system out. See this page for details. You can use this command in Jenkins to retrieve the coverage and unit test results from your container after the build completes.

Firstfoot answered 28/3, 2017 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.