在 gitlab-ci 中停止启动的后台服务 (phantomjs)

Stopping a started background service (phantomjs) in gitlab-ci

作为我工作的一部分,我正在使用 specific 参数启动 phantomjs。

这是在自定义 gitlab/gitlab-ci 服务器上 运行ning,我目前没有使用容器,我想这会简化它。

我是这样开始 phantomjs 的:

- "timeout 300 phantomjs --ssl-protocol=any --ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768 2>&1 >> /tmp/gastonjs.log &"

然后我 运行 进行行为测试,然后再次停止该过程:

- "pkill -f 'src/Client/main.js' || true"

问题是当 behat 测试失败时,它不会执行 pkill 并且 test-运行 卡在等待 phantomjs 完成。我已经添加了超时 300 但这意味着我目前仍在等待失败后 2 分钟左右,它最终会停止它,而测试仍然 运行ning 当它们变得足够慢时。

我还没有找到 运行 某种 post-run/cleanup 命令的方法,在失败的情况下也会 运行s。

有更好的方法吗?我可以以 gitlab-ci 不关心它仍然是 运行ning 的方式启动 phantomjs 吗?不,也许?

TL;DR; - 使用 & 在新线程中生成进程,但随后您必须确保进程在成功和失败构建中被终止。

我用这个(带评论):

'E2E tests':
  before_script:
    - yarn install --force >/dev/null
    # if there is already an instance running kill it - this is ok in my case - as this is not run very often
    - /bin/bash -c '/usr/bin/killall -q lite-server; exit 0'
    - export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print  }' | cut -f2 -d ':' | head -n1)
    - export E2E_BASE_URL="http://$DOCKERHOST:8000/#."
    # start the lite-server in a new process
    - lite-server -c bs-config.js >/dev/null &
  script:
    # run the tests
    - node_modules/.bin/protractor ./protractor.conf.js --seleniumAddress="http://localhost:4444/wd/hub" --baseUrl="http://$DOCKERHOST:8000" --browser chrome
    # on a successfull run - kill lite server
    - killall lite-server >/dev/null
  after_script:
    # when a test fails - try to kill it in the after_script. this looks rather complicated, but it makes sure your builds dont fail when the tests succeedes and the lite-server is already killed. to have a successfull build we ensure a non-error return code (exit 0)
    - /bin/bash -c '/usr/bin/killall -q lite-server; exit 0'
  stage: test
  dependencies:
    - Build
  tags:
    - selenium

https://gist.github.com/rufinus/9ee8f04fc1f9248eeb0c73ad5360a006#file-gitlab-ci-yml-L7

尝试-9信号:

- "pkill -9 -f 'src/Client/main.js' || true"

你也可以尝试其他信号,你可以找到一个列表here

正如提示的那样,基本上我的问题不是我无法终止进程,而是 运行 我的测试脚本失败并在该点停止,导致死锁。

我已经在做一些与@Rufinus 的示例非常相似的事情,但它对我不起作用。可能会有一些不同的事情,比如 运行 测试的不同方式或者在 before_script 中启动它,这对我来说不是一个选项。

我确实找到了一种方法让它为我工作,这是为了防止我的测试运行器停止执行进一步的任务。我设法用 "set +e" 做到了这一点,然后存储了退出代码(我以前尝试过但没有成功)。

这是我工作中的相关部分:

# Set option to prevent gitlab from stopping if behat fails.
- set +e
- "phantomjs --ssl-protocol=any --ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768 2>&1 >> /dev/null &"
# Store the exit code.
- "./vendor/bin/behat -f progress --stop-on-failure; export TEST_BEHAT=${PIPESTATUS[0]}"
- "pkill -f 'src/Client/main.js' || true"
# Exit the build
- if [ $TEST_BEHAT -eq 0 ]; then exit 0; else exit 1; fi