当pytest作业测试失败时,如何使管道阶段网格显示为不稳定

How to make pipeline stage grid be displayed as Unstable when the pytest job has failed tests

我有 4 个 jenkins 作业,都是关于 pytest 的,每个作业的 shell 命令就像

pytest -v --junitxml=result.xml || exit 0

我加 || exit 0的原因是因为我希望每一个工作运行一个接一个,即使任何一个工作失败

然后我创建了一个管道作业,管道脚本如下

node {
    stage('win10_chrome90_admin') {
        build job: 'UI_automation_win10_chrome90_admin', propagate: false
    }
    stage('win10_chrome90_salesman') {
        build job: 'UI_automation_win10_chrome90_salesman', propagate: false
    }
    stage('win10_firefox88_admin') {
        build job: 'UI_automation_win10_firefox88_admin', propagate: false
    }
    stage('win10_firefox88_salesman') {
        build job: 'UI_automation_win10_firefox88_salesman', propagate: false
    }
}

所以每次当我 运行 管道时,舞台视图总是绿色的,我无法判断是否有任何作业测试失败,实际上图像中的第二个作业在点击它后显示为不稳定工作视图。

有什么办法可以让job的网格在测试失败时显示为黄色(不稳定)?(和我点进去job后看到的一样)

这种情况下可以使用catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'),这样流水线就成功了,但是stage显示失败了。 您可以选择 buildResult 和 stageResult 的状态,以防您希望它不稳定或失败。
示例管道: 在下面的示例中,即使“Testing”阶段失败,其他阶段仍将执行,并且“Testing”阶段将在视图中标记为失败。

stage("Testing") {
          steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                script {
                         
                        // You can execute your commands without || exit 0
                   }
                }
            }

编辑 :您还可以通过以下方式添加它来实现不稳定的行为:

pipeline {
    agent any
    stages {
        stage('win10_chrome90_admin') {
            steps {
               script{
                try {
                
                    build job: 'UI_automation_win10_chrome90_admin'
                    }
                     catch (e){
                   unstable('win10_chrome90_admin failed!')
                   }
               }
            }
        }

        stage('win10_chrome90_salesman') {
            steps {
                script{
                try {
                    build job: 'UI_automation_win10_chrome90_salesman'
                }
                catch (e){
                   unstable('win10_chrome90_salesmanfailed!')
                   }
               }
          }
        }
        stage('win10_firefox88_admin') {
            steps {
              script{
                try {
                
                    build job: 'UI_automation_win10_firefox88_admin'
                    }
                     catch (e){
                   unstable('UI_automation_win10_firefox88_adminfailed!')
                   }
               }  
              }
            }
        }
    }
}

在pytest中使用pytest插件pytest-custom-exit-code,所以我的测试作业中的shell命令类似于

pytest -v --junitxml=result.xml --suppress-tests-failed-exit-code

在我的管道作业中,管道脚本类似于

pipeline {
    agent any
    stages {
        stage('win10_chrome90_admin') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                    build job: 'UI_automation_win10_chrome90_admin'
                }
            }
        }

        stage('win10_chrome90_salesman') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                    build job: 'UI_automation_win10_chrome90_salesman'
                }
            }
        }
        stage('win10_firefox88_admin') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                    build job: 'UI_automation_win10_firefox88_admin'
                }
            }
        }
    }
}

最后是我的管道(运行 4个测试作业一个一个) 运行 如下所示,如果任何测试作业测试失败,舞台网格将显示不稳定(黄色),并且下一个工作将继续推出