Jenkins build inside a docker container with generated reports

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 16:24:58

问题


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.


回答1:


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:

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.




回答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.



来源:https://stackoverflow.com/questions/38476811/jenkins-build-inside-a-docker-container-with-generated-reports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!