Jenkins使用生成的报告在docker容器中构建 [英] Jenkins build inside a docker container with generated reports

查看:225
本文介绍了Jenkins使用生成的报告在docker容器中构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Jenkins和Docker的新手,即使经过一些研究,我仍然找不到解决这些问题的方法.

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

我要:

  • 在Docker容器中的项目上执行pytest和python-coverage.
  • 这应该生成测试和覆盖率报告
  • 访问生成的报告,并使用一些Jenkins的插件阅读它们.

当我在Docker本地尝试时,它正在工作.我创建了一个Dockerfile,该文件创建了一个带有所需libs和其中源代码的docker映像,然后在创建容器并运行测试时调用一个脚本.我可以看到它正在工作,因为我使用了cat并能够在终端中查看生成的报告.

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.

我的问题是:如何访问Jenkins容器内生成的报告,然后使用插件读取它们.

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.

首先,我的Dockerfile示例:

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

我的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

当我使用Cloudbees插件运行它时,这是我的jenkins日志:

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

因此,我的主要目标是为詹金斯找到一种访问生成的报告的方法.另外,有没有办法在构建时查看docker容器内部发生了什么?例如,当我在本地尝试时,我尝试在外壳脚本中放入cat来查看报告,但是在Jenkins中,我找不到找到它的方法.

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.

推荐答案

Docker插件 Docker Slaves插件应该可以正常工作美好的.

Either Docker Plugin or Docker Slaves Plugin should work fine.

只需确保生成的报告采用Jenkins可以理解"的xUnit格式,并添加一个后构建操作即可发布测试结果,指向正确的路径,如下所示:

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会将生成的报告复制到容器外部.

Jenkins will copy that generated report to outside the container.

如果覆盖率工具正在生成HTML报告,则可以使用 HTML就像上述发布步骤一样,发布者插件.

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

这篇关于Jenkins使用生成的报告在docker容器中构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆