从shell脚本中返回一个结果值,用于在Docker容器中的testing给Jenkins CI

我想使用Jenkins CI来执行我的自动end2endtesting。 我的testing与Nightwatch.js一起运行。 我想通过shell脚本在自创的docker容器中运行我的自动化testing。 这个shell脚本在本地机器上完美运行,但是如果我在Jenkins CI启动这个shell脚本,如果testing通过或失败,shell脚本必须返回一个值。 如果他们通过jenkins的工作也必须通过。 而当Docker容器中的testing失败时,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 (eg 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的工作有关testing状态的信息? 我知道这应该是困难的,因为我执行一个shell脚本启动Docker容器并执行这个Docker容器中的testing。

您可能需要从我的答案下面唯一可能是:
exit $?
$? 给你你以前的命令/脚本的返回值

最佳答案:
我有一个类似的设置,我如何处理jenkins里面的Docker容器中执行夜间testing:

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

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

但是实际上我不认为在执行testing时转发shell脚本的退出代码是一个好习惯。 我也为这项工作做了什么,我开始使用-v ${WORKSPACE}/reports:/nightwatch/reports参数创build容器,并且添加了生成后操作Publish JUnit test result report并指定要包含的报告文件夹。 这有助于您获得有关您的系统健康的更好的信息。

希望能帮到你! 🙂