【问题标题】:How to abort Jenkins pipeline stage after failed tests测试失败后如何中止Jenkins管道阶段
【发布时间】:2018-08-07 13:10:21
【问题描述】:

我使用 Jenkins 来运行自定义 BDD 框架。它目前设置为每天运行,整个测试套件每次都运行。现在我正在尝试以这种方式设置我的管道(使用声明性管道),因此我将有几个日常运行阶段:预检查、主运行、报告发送等。我的目标是进行 预检查阶段,这将显示几个关键功能正常工作,然后 - 运行主套件。如果我的预检查方案将失败,我需要中止下一阶段时遇到了问题。到目前为止我还没有找到解决方案,所以如果有人能分享一个想法,我将不胜感激。 问题是执行 shell 命令总是成功的,测试套件不会失败,只是测试场景可能会失败。因此,根据这一点,我需要继续/中止下一阶段。 这就是我的管道文件现在的样子:

...

pipeline {
   agent {
    ...
   }
   environment {
    ...
   }
   options {
    ...
   }
   triggers {
    ...
   }
   parameters {
    ...
   }
   stages {
    stage('Setup Environment') {
        steps {
            script {
                ...
            }
        ...
        }
    }

    stage('Phase 1: Pre-run Check') {
        steps {
            sh "bash -c '. ~/.rvm/scripts/rvm; bin/hal test ${hal_env} ${params.Browser} --tags @phase-1-check'"
        }
    }
    stage('Test On Firefox') {
        parallel {
            stage('Runner A') {
                steps {
                    sh "bash -c '. ~/.rvm/scripts/rvm; bin/hal test ${hal_env} ${params.Browser} --tags @phase-2-test --retry 2 --parallel [1,4] || true'"
                }
            }
            ...
        }
    }
    stage('Send Daily results') {
        steps {
            ...
        }
    }
   }
   post {
    always {
        ...
    }
   }
  }
  ...

【问题讨论】:

    标签: selenium jenkins automation jenkins-pipeline bdd


    【解决方案1】:

    您能否确保您的测试套件在测试失败时返回非零退出代码?

    【讨论】:

      【解决方案2】:

      我无法让我的测试套件返回退出代码,但我使用了 '| tee output' 命令将日志输出到一个文件,然后我可以读取该文件(使用 awk 命令)并在日志中查找故障。 所以问题解决了,这是我的解决方案: '''

      def phase1_status;
      ...
      stage('Phase 1: Pre-run Check') {
                  steps {
                      sh "bash -c '<Start Command> | tee hal_output'"
                      script {
                        phase1_status = sh( returnStatus: true, script: "awk \'/scenario.*failed/ {exit 1}\' hal_output")
                      }
                  }
      }
      stage('Test On Firefox') {
                  when {
                    equals expected: 0, actual: phase1_status
                  }
      ...
      

      '''

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 1970-01-01
        • 2020-04-01
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多