5

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.

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?

many thanks

Dave Sinclair

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
user2187357
  • 81
  • 1
  • 1
  • 2
  • Check here the answer of Jason http://stackoverflow.com/questions/9664716/archiving-artifacts-not-in-the-workspace-when-build-fails – Ruslan Zasukhin Jun 05 '15 at 18:11

3 Answers3

0

An inelegant way out will be to use the "Post Build task plugin". It can parse the build log. So you can set it to check for failure logs statements. Once found you can launch a script. Maybe you can trigger second build via this script.

I am sure that there will be some other elegant options out there in post build tasks ecosystem. I am watching this question to see Jenkins experts to answer it.

NotAgain
  • 1,927
  • 3
  • 26
  • 42
0

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
Nebula
  • 1,045
  • 2
  • 11
  • 24
0

https://plugins.jenkins.io/postbuildscript/ should do the job. It allows to run a script if the build failed or even got aborted.