将shell脚本中的结果值从Docker容器中的测试返回到Jenkins CI

时间:2017-07-19 20:01:16

标签: bash shell docker jenkins nightwatch.js

我想使用Jenkins CI来执行我的自动end2end测试。我的测试与Nightwatch.js一起运行。我想通过shell脚本在自己创建的docker容器中运行我的自动化测试。这个shell脚本在本地机器上完美运行,但是如果我在Jenkins CI中启动这个shell脚本,那么如果测试通过或失败,shell脚本必须返回一个值。如果他们通过詹金斯工作也必须通过。当Docker容器中的测试失败时,Jenkins作业也必须失败。

这是我当前的shell脚本:

#run the end2end tests headless in docker container from local with remote repository
#parameter: $1 folder on local machine to copy in project sources (with cucumber reports after test execution)
#              (IMPORTANT: You have to ensure that the given folder path is in the file sharing paths in Docker configuration!!!)
#           $2 git url with credentials to GitLab repo (e.g. https://<username>:<password>@gitlab.com/hrsinnolab/e2e-web-tests.git)
#           $3 defined the running browser (scipts in package.json)
#           $4 branch to run the tests against (optionally / if empty then the 'master' is used for tests as default)
#examples:
# run the tests with chrome against the branch 'NIKITA-1234'
#./runTestsLocal /Users/me/e2e-tests/reports/ https://me:mypassword@gitlab.com/hrsinnolab/e2e-web-tests.git test-chrome NIKITA-1234
# run the tests with firefox against the default 'master'
#./runTestsLocal /Users/me/e2e-tests/reports/ https://me:mypassword@gitlab.com/hrsinnolab/e2e-web-tests.git test-firefox
docker_image=grme/nightwatch-chrome-firefox:0.0.2
echo "------ stop all Docker containers ------" \
&& (docker stop $(docker ps -a -q) || echo "------ all Docker containers are still stopped ------") \
&& echo "------ remove all Docker containers ------" \
&& (docker rm $(docker ps -a -q) || echo "------ all Docker containers are still removed ------") \
&& echo "------ pull Docker image '"$docker_image"' from Docker Cloud ------" \
&& docker pull "$docker_image" \
&& echo "------ start Docker container from image ------" \
&& docker run -d -t -i -v $1:/my_tests/ "$docker_image" /bin/bash \
&& echo "------ execute end2end tests on Docker container ------" \
&& docker exec -it $(docker ps --format "{{.Names}}") bash -c \
  "rm -Rf /my_tests/project \
  && git clone $2 /my_tests/project \
  && cd /my_tests/project \
  && git checkout $4 \
  && npm install \
  && npm install -y nightwatch-cucumber@7.1.10 \
  && npm install -y chromedriver@2.30.1 \
  && npm install -y geckodriver@1.7.1 \
  && npm install -y cucumber-html-reporter@2.0.3 \
  && npm install -y multiple-cucumber-html-reporter@0.2.0 \
  && xvfb-run --server-args='-screen 0 1600x1200x24' npm run $3" \
&& echo "------ cleanup all temporary files ------" \
&& rm -Rf $1/project/tmp-* \
&& rm -Rf $1/project/.com.google* \
&& echo "------ stop all Docker containers again ------" \
&& (docker stop $(docker ps -a -q) || echo "------ all Docker containers are still stopped ------") \
&& echo "------ remove all Docker containers again ------" \
&& (docker rm $(docker ps -a -q) || echo "------ all Docker containers are still removed ------")

如何返回值以向Jenkins作业提供有关测试状态的信息?我知道它应该很难,因为我执行了一个shell脚本,它启动了Docker容器并在这个Docker容器中执行测试。

1 个答案:

答案 0 :(得分:0)

下面我的回答你唯一可能需要的是:
exit $?
$?为您提供上一个命令/脚本的返回值

完整答案:
我有一个类似的设置,我如何处理在jenkins里面的docker容器中执行nightwatch测试:

RC=0
docker exec -i ${DOCKER_NIGHTWATCH} node_modules/.bin/nightwatch --suiteRetries 1 || RC=$?
exit $RC

容器显然是事先使用-dsleep启动的。

但实际上我并不认为在执行测试时从shell脚本中继出代码是一个好习惯。我也为这项工作做的是,我使用-v ${WORKSPACE}/reports:/nightwatch/reports参数启动了容器,并添加了一个构建后操作Publish JUnit test result report并指定要包含的报告文件夹。这有助于您获得有关您的系统健康状况的更好信息。

希望对你有所帮助! :)