【问题标题】:How to get the output of the command maven如何获取命令maven的输出
【发布时间】:2018-10-21 13:15:15
【问题描述】:

我关注Jenkinsfile

node {
  stage('Checkout') {
    checkout scm
  }
  stage('Build') {
    try {
      sh ''' mvn clean -B org.jacoco:jacoco-maven-plugin:prepare-agent install  > commandResult '''
      def result = readFile('commandResult').trim()
    }
    catch (err) {
      step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1, thresholds: [
        [$class: 'FailedThreshold', failureThreshold: '', unstableThreshold: '1'],
        [$class: 'SkippedThreshold', failureThreshold: '', unstableThreshold: '']
      ], tools: [
        [$class: 'JUnitType', deleteOutputFiles: false, failIfNotNew: false, pattern: '**/target/surefire-reports/TEST-*.xml', skipNoTestFiles: true, stopProcessingIfError: false]
      ]])
    }
    finally {
      echo "{$result}"
    }
  }
}

当某些测试失败时,我正在使用插件xUnit 设置不稳定构建的阈值(在这种情况下,当一个或多个测试失败时,构建被标记为不稳定)。如果出现编译错误,我还想解析 Maven shell 命令的输出以将构建标记为“失败”。如果没有错误并且所有测试都通过,则继续执行 xUnit。我怎样才能做到这一点?

我已经尝试过其他问题中提供的答案,但在我的情况下不起作用。当我在上面尝试这个Jenkinsfile 时,构建失败并且我没有得到输出值。

我的 Jenkins 版本是 2.107.2

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline xunit


    【解决方案1】:

    默认情况下,如果传递给sh 步骤的脚本存在且退出代码非零,则管道步骤将失败并引发异常。在这种情况下,您读取文件的下一行代码不会被执行。

    Jenkins 的sh 步骤支持returnStatus 选项。如果将其设置为true,它将使sh 步骤返回退出代码,并且如果此退出代码不是零,它不会抛出任何异常。但是在这种情况下,如果退出代码通知您有关错误,您必须做出反应。考虑以下示例:

    stage ('Build') {
      def exitCode = sh(script: 'mvn clean -B org.jacoco:jacoco-maven-plugin:prepare-agent install  > commandResult', returnStatus: true)
      def result = readFile('commandResult').trim()
    
      if (exitCode != 0) {
        // error happened 
        step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1, thresholds: [
          [$class: 'FailedThreshold',  failureThreshold: '', unstableThreshold: '1'],
          [$class: 'SkippedThreshold', failureThreshold: '', unstableThreshold: '']
        ],tools: [
          [$class: 'JUnitType', deleteOutputFiles: false, failIfNotNew: false, pattern: '**/target/surefire-reports/TEST-*.xml', skipNoTestFiles: true, stopProcessingIfError: false]
        ]])
      }
    
      // do something with result
      echo "{$result}"
    
      // e.g mark build as error
      error 'Maven command failed!'
    }
    

    参考:https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script

    使用mvn --log-file 代替>

    你也可以试试:

    stage ('Build') {
      def exitCode = sh(script: 'mvn --log-file commandResult clean -B org.jacoco:jacoco-maven-plugin:prepare-agent install', returnStatus: true)
      def result = readFile('commandResult').trim()
    
      if (exitCode != 0) {
        // error happened 
        step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1, thresholds: [
          [$class: 'FailedThreshold',  failureThreshold: '', unstableThreshold: '1'],
          [$class: 'SkippedThreshold', failureThreshold: '', unstableThreshold: '']
        ],tools: [
          [$class: 'JUnitType', deleteOutputFiles: false, failIfNotNew: false, pattern: '**/target/surefire-reports/TEST-*.xml', skipNoTestFiles: true, stopProcessingIfError: false]
        ]])
      }
    
      // do something with result
      echo "{$result}"
    
      // e.g mark build as error
      error 'Maven command failed!'
    }
    

    【讨论】:

    • 我试过这个并且构建在读取文件时中止:Read file from workspace -- commandResult -- (self time 143ms)
    • 你能粘贴那个版本的控制台日志吗?
    • Stage Logs (Build) Shell Script (self time 5s) [jenkins-failure_master-EHKMAGGDJWHA7FNGHQYY2VJNJHBX2RXI3XJY4NU5EB2PYT4ERTZQ] Running shell script + mvn clean -B org.jacoco:jacoco-maven-plugin:prepare-agent install Read file from workspace -- commandResult -- (self time 143ms)
    • 变量exitCode等于1。我猜文件没有创建?
    • 这不起作用,因为当测试失败时,退出代码也是 1。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2016-04-30
    • 2012-09-23
    • 1970-01-01
    • 2011-04-30
    • 2014-08-02
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多