You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins声明式流水线并行单元测试阶段错误处理配置问题

问题分析与解决方案

你的代码核心问题在于手动try/catch吞掉了异常,导致Jenkins无法识别阶段失败,所以阶段显示绿色;同时缺少对后续Analysis阶段的跳过逻辑。以下是符合需求的修改方案:

修改后的完整Jenkinsfile

@Library('jenkins-shared-lib')_

def unitTestsFailed = false // 全局变量跟踪单元测试是否失败

pipeline {
  agent none

  stages {
    stage('init') {
      agent { label 'jenkins-01' }
      steps {
        script {
          // code for loading env and scripts
        }
      }
    }

    stage('UnitTests') {
      parallel {
        stage('UnitTest-Stage1') {
          agent { label 'jenkins-01' }
          steps {
            script {
              catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                unitTest.function1()
              }
              // 如果当前子阶段失败,标记全局变量
              if (currentBuild.currentResult == 'FAILURE') {
                unitTestsFailed = true
              }
            }
          }
          post {
            success { script { notifyTeams() } }
            failure { 
              script {
                errMsg = currentBuild.rawBuild.getLog(50).join('\n') // 获取错误日志
                notifyTeams()
              }
            }
          }
        }

        stage('UnitTest-Stage2') {
          agent { label 'jenkins-2' }
          steps {
            script {
              catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                unitTest.function3()
              }
              if (currentBuild.currentResult == 'FAILURE') {
                unitTestsFailed = true
              }
            }
          }
          post {
            success { script { notifyTeams() } }
            failure { 
              script {
                errMsg = currentBuild.rawBuild.getLog(50).join('\n')
                notifyTeams()
              }
            }
          }
        }

        stage('UnitTest-Stage3') {
          agent { label 'jenkins-1' }
          steps {
            script {
              catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                unitTest.function4()
              }
              if (currentBuild.currentResult == 'FAILURE') {
                unitTestsFailed = true
              }
            }
          }
          post {
            success { script { notifyTeams() } }
            failure { 
              script {
                errMsg = currentBuild.rawBuild.getLog(50).join('\n')
                notifyTeams()
              }
            }
          }
        }
      }
      post {
        success { script { notifyTeams() } }
        failure { 
          script {
            notifyTeams()
            // 确保构建结果标记为失败
            currentBuild.result = 'FAILURE'
          }
        }
      }
    }

    stage('Analysis') {
      // 只有单元测试全部成功时才执行
      when { expression { !unitTestsFailed } }
      parallel {
        stage('Analysis-Stage1') {
          steps {
            script {
              sq.function1()
            }
          }
          post {
            success { script { notifyTeams() } }
            failure { 
              script {
                errMsg = currentBuild.rawBuild.getLog(50).join('\n')
                notifyTeams()
                currentBuild.result = 'FAILURE'
              }
            }
          }
        }
      }
    }
  }
}

关键修改说明

  • 全局状态跟踪:定义unitTestsFailed变量,所有单元测试子阶段失败时将其设为true,用于后续判断是否跳过Analysis阶段。
  • 移除多余try/catch:直接用catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')包裹测试逻辑,既捕获异常不终止并行,又能正确标记子阶段为失败(红色),同时将构建结果设为FAILURE。
  • 子阶段失败判断:在每个子阶段步骤后检查当前阶段结果,更新全局失败标记。
  • Analysis阶段跳过逻辑:通过when { expression { !unitTestsFailed } }实现,只要单元测试有失败就跳过整个Analysis阶段。
  • 错误日志获取优化:用currentBuild.rawBuild.getLog(50)获取最近50行日志作为错误信息,替代原来的caughtException(原方式可能因变量作用域问题失效)。

内容的提问来源于stack exchange,提问作者mair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:09:55