在Jenkins上使用声明性管道时,根据jenkins管道文档,超时块应该中止整个作业。在我的例子中,超时似乎发生在运行一组mocha测试的shell脚本中,而mocha将SIGTERM转换为一个测试错误。当错误返回到jenkinsfile时,org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
错误不再可捕获,作业也不会中止。编辑:原因似乎是摩卡的并行功能,但不确定如何处理。
下面是mocha (版本9)在超时时抛出的错误。
1) Uncaught error outside test suite:
Uncaught Workerpool Worker terminated Unexpectedly
exitCode: `null`
signalCode: `SIGTERM`
workerpool.script: `/var/lib/jenkins/workspace/jobName/path/node_modules/mocha/lib/nodejs/worker.js`
spawnArgs: `/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/Node12.14.0/bin/node,/var/lib/jenkins/workspace/jobName/path/node_modules/mocha/lib/nodejs/worker.js`
spawnfile: `/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/Node12.14.0/bin/node`
stdout: `null`
stderr: `null`
Error: Workerpool Worker terminated Unexpectedly
exitCode: `null`
signalCode: `SIGTERM`
spawnfile: `/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/Node12.14.0/bin/node`
stdout: `null`
stderr: `null`
at ChildProcess.<anonymous> (node_modules/workerpool/src/WorkerHandler.js:292:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
下面是正在运行的jenkisfile的示例。
pipeline {
agent any
stages {
stage("run-tests") {
steps {
script {
try {
timeout (60) {
sh "npm run mocha-tests"
}
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
echo "interrupt ${e.toString()}"
} catch (Throwable e) {
echo "regular ${e.toString()}"
}
}
}
}
}
我希望工作在超时时中止。我能做些什么来确保它流产呢?
发布于 2022-09-01 19:52:36
我手头没有摩卡项目,但看起来摩卡检测到SIGTERM事件和错误,就像你说的。这反过来会导致"sh“步骤停止"on-error”,而不是被超时终止。超时希望“中止”作业,但不会导致错误。我想这就是为什么“错误”状态获胜的原因。
您可能会尝试返回"sh“的退出代码,这样也会阻止它退出。然后,您可以检查npm/mocha结果,以防您在超时后感兴趣.
script {
try {
timeout (60) {
teststate = sh returnStatus: true, script: "npm run mocha-tests"
}
}
catch (...) {}
}
https://stackoverflow.com/questions/69527388
复制相似问题