简体   繁体   English

如何在测试失败时停止 jenkins 管道构建 - 并将其标记为不稳定

[英]How to stop a jenkins pipeline build when tests fail - and mark it unstable

So I have a multi stage pipeline build.所以我有一个多阶段的管道构建。

One of those stages run pytest - a subsequent stage picks up the test result.其中一个阶段运行 pytest - 后续阶段获取测试结果。

As it stands because there are test failures pytest returns a non zero exit code causing jenkins to stop at that step and mark the build failed (red)就目前而言,因为存在测试失败 pytest 返回非零退出代码,导致 jenkins 在该步骤停止并标记构建失败(红色)

If I don't return the none zero exit code the build is marked unstable as there are failing tests.如果我不返回非零退出代码,则构建被标记为不稳定,因为有失败的测试。

However, the build is not stopped at that point.但是,构建并没有在那时停止。

Now what I want to do is to be able to stop the build at that point so I don't call any subsequent stages after a test failure... but not have the build marked failed.现在我想做的是能够在那时停止构建,所以我不会在测试失败后调用任何后续阶段......但没有将构建标记为失败。 I do however want the post stage to still run and then have the build marked unstable (as there was not an error but only a test failure).但是,我确实希望后期阶段仍然运行,然后将构建标记为不稳定(因为没有错误,只有测试失败)。

Ideally I'd like a pipeline command "stopBuild(unstable)" or something like that.理想情况下,我想要一个管道命令“stopBuild(unstable)”或类似的东西。

The "fail build on test failure" is nice because it stops the build - ie does not run any subsequent stages. “在测试失败时构建失败”很好,因为它会停止构建 - 即不运行任何后续阶段。 I want this behaviour when I detect a test has failed as well.当我检测到测试失败时,我也想要这种行为。

Default behaviour when a test failure is detected is to continue running the build.检测到测试失败时的默认行为是继续运行构建。

I don't really want to have to add another stage to stop subsequent stages from running... and I certainly don't want to have to put a filter in any subsequent stage.我真的不想添加另一个阶段来阻止后续阶段运行......我当然不想在任何后续阶段中放置过滤器。

You can add when under your following stages, this way, all stages after your test stage will be skipped.您可以在以下阶段下添加 when,这样,您的测试阶段之后的所有阶段都将被跳过。

Eg:例如:

stage('Example') {
            when {
                expression {currentBuild.result != 'UNSTABLE'}
            }
            steps {
                echo 'Test passed so i\'m running'
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM