Jenkins在生成报告的Docker容器内部构build

我对Jenkins和Docker是新手,甚至经过一番研究,我都没有办法去做这些事情。

我要 :

  • 在Docker容器中的项目上执行pytest和python-coverage。
  • 这应该产生testing和覆盖报告
  • 访问生成的报告,并阅读一些Jenkins'plugin。

当我用Docker在本地尝试时,它正在工作。 我创build了一个Dockerfile,它创build一个Docker镜像,其中包含所需的库和其中的源代码,然后在创build容器并运行testing时调用脚本。 我可以看到它正在工作,因为我使用猫,并能够看到我的terminal内生成的报告。

我的问题是:我怎样才能访问在jenkins容器内生成的报告,并阅读插件后。

编辑:所以这里的一个例子,我正在尝试这样做,你可以有一个更好的主意。

首先,我的Dockerfile示例:

# 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包含我的testing执行

 # 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插件运行时,我的jenkinslogging:

 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 

所以我的主要目标是find一个让jenkins得到访问报告的方法。 另外,有没有办法看到什么是在docker集装箱内build设? 例如,我尝试在我的shell脚本中放入一个cat以便在本地尝试时查看报告,但在Jenkins中,我找不到一个方法来查看它。

Docker Plugin或Docker Slaves Plugin应该可以正常工作。

只要确保生成的报告是xUnit格式,Jenkins可以“理解”并添加Post-build Action来发布指向正确path的testing结果,如下所示:

发布JUnit测试

Jenkins会将生成的报告复制到容器外部。

如果您的报道工具正在生成HTML报告,则可以使用HTML发布者插件 ,就像上面介绍的发布步骤一样。

Docker的副本(docker cp)可以将容器文件系统中的文件复制出来。 详情请参阅此页面 。 在构build完成后,您可以在Jenkins中使用此命令从容器中检索覆盖率和unit testing结果。