仅在上一步失败时如何在Jenkins中运行条件步骤 [英] How to run a conditional step in Jenkins only when a previous step fails

查看:89
本文介绍了仅在上一步失败时如何在Jenkins中运行条件步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在詹金斯(Jenkins)中创建一个两步作业.我希望仅在第一步失败的情况下才能运行第二步. (第一步执行一个单元测试,以查看我正在编译的代码是否良好-如果不是,那么我要在第二步中运行一些诊断程序.)

I'm trying to create a 2 step job in Jenkins. I want the second step only to run if the first step fails. (The first step executes a unit test to see if the code I'm compiling is good - if it is not then i want to run some diagnostics in the second step).

条件步骤插件似乎是一个不错的选择.但是,我无法弄清楚在第一步失败时如何使用条件步骤插件使第二步运行.

The conditional step plug seems a good choice for this. However, I can't work out how to use the conditional step plug in to cause the second step to run when the first step fails.

条件步骤插件提供条件列表,例如从不",布尔条件" ...和当前构建状态".我本以为我应该使用当前构建状态"条件,并将最坏的状态设置为失败",将最好的状态设置为不稳定".然后,如果第一步失败,则第二步将运行.

The conditional step plug in offers a list of conditions such as 'Never', 'Boolean condition' ... and 'Current Build Status'. I would have thought I should use a 'Current Build Status' condition and set the worst status to 'failed' and the best status to 'unstable'. Then if the first step fails, the second step will run.

但是,这似乎有问题.当第一步失败时,詹金斯会立即停止工作.因此,第二步永远没有机会评估其状况并查看其是否应该运行.

However there seems to be a problem with this. When the first step fails, then Jenkin's stops the job at the point. So the second step never has a chance to evaluate its condition and see whether or not it should run.

所以我看不到如何在条件步骤插件中使用失败的当前构建状态"-好像构建已经失败,我们再也无法进入条件步骤了.如何在第1步中将构建状态标记为失败但没有Jenkins然后在那一点上停止工作?

So I can't see how the use of a 'Current build status' of failed could ever be used in the conditional step plugin - as if the build has already failed, we never get to the Conditional Step. How can one mark the build status as failed in step 1 but not have Jenkins then stop the job at that point?

非常感谢

戴夫·辛克莱尔

推荐答案

您可以创建始终成功的脚本构建步骤.在该脚本中,您可以在一切正常时创建一个文件,在出现问题时创建另一个文件.您可以在两个单独的条件构建步骤中检查文件是否存在.例如:

You can create a script build step that always succeeds. In that script you could create one file when everything is right, another when something went wrong. You can check for the existence of the files in two separate conditional build steps. For instance:

REM Clean up flags
del build_failed
del build_ok

REM This should be the actual buildstep

REM Foobar does not exist, so it will return an error. dir will always work. Exchange the     REM between the two lines to test both scenarios
dir foobar
REM dir

REM Set the flags based on the build step status
IF NOT %ERRORLEVEL% == 0 (
    echo  > build_failed
) ELSE (
    echo  > build_ok
)

REM Ensure that this step will always pass
EXIT 0

这篇关于仅在上一步失败时如何在Jenkins中运行条件步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆