无法捕获org.jenkinsci.plugins.workflow.steps.超时时出现流程中断异常

rqenqsqc  于 2022-11-02  发布在  Jenkins
关注(0)|答案(1)|浏览(361)

当在Jenkins上使用声明性管道时,根据jenkins管道文档,超时块应该中止整个作业。在我的例子中,超时似乎发生在运行一套mocha测试的shell脚本期间,mocha将SIGTERM转换为测试错误。当错误返回到jenkinsfile时,org.jenkinsci.plugins.workflow.steps.FlowInterruptedException错误不再可捕获,并且作业不会放弃。编辑:看起来原因是mocha的并行功能,但不知道如何处理。
下面是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)

下面是正在运行的jenkis文件的示例。

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()}"
                        }
                    }
            }
    }
}

我希望作业在超时时中止。如何确保作业中止?

ruyhziif

ruyhziif1#

我手头没有mocha项目,但看起来像是mocha检测到SIGTERM事件并出错了,就像你说的那样。这反过来应该会导致“sh”步骤停止“on-error”,而不是被超时终止。超时想要“Abort”作业,但不想导致错误。我猜这就是为什么“error”状态获胜。
你可以尝试返回“sh”的退出代码,这样也可以防止它退出。然后你可以检查npm/mocha的结果,以防你在超时后感兴趣...

script {
  try {
    timeout (60) {
      teststate = sh returnStatus: true, script: "npm run mocha-tests"
    }
  } 
  catch (...) {}
}

相关问题