How to execute a conditional step in Jenkins only when the previous step failed

I am trying to create a two-stage job in Jenkins. I want the second step to be performed only if the first step failed. (The first step runs a unit test to make sure that the code I'm compiling is good - if it isn't, I want to run some diagnostics in the second step).

The conditional step plug seems to be a good choice for this. However, I cannot figure out how to use the conditional step plug to force the second step to start when the first step fails.

The conditional step step offers a list of conditions, such as "Never", "Logical condition" ... and "Current build status". I would have thought that I should use the condition “Current build state” and set the worst status to “failure” and the best status to “unstable”. Then, if the first step fails, the second step will be performed.

However, there seems to be a problem with this. When the first step fails, then Jenkin stops work at this point. Thus, in the second stage it will never be possible to assess his condition and see if it will work.

Thus, I do not see how the use of the “current assembly status” can be used in the plugin of the conditional step - as if the assembly had already failed, we never get to the Conditional step. How can I mark the build status as unsuccessful in step 1, but did not start Jenkins work at this point?

many thanks

Dave Sinclair

+3
source share
2 answers

An ineffective solution would be to use the Post Build Job Plugin. It can analyze the build log. Thus, you can install it to check reports of failed logs. After detection, you can run the script. Perhaps you can run the second build through this script.

, post build tasks. , Jenkins, .

0

script, . script , , , - . . :

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
0

All Articles