How to run a conditional step in Jenkins only when a previous step fails - 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.
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

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.

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

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

Related

Jenkins - How to make other build steps run even if previous build step fail

I have multiple build steps in my Jenkins Free style Job.
If one of the build step fails, the the following build step doesn't get triggered.
I am Ok if the 1st build step fails as these are tests and could fail & would like to retain the $BUILD_STATUS value for reporting, but I still want the 2nd build step to run & not be skipped because build step 1 failed.
I also tried to combine both the build steps together into one but its still the same - the commands from the 2nd build step do not get executed if the commands in the 1st build step fail.
Thanks
Sa
Jenkins pipelines separates your pipeline into stages and steps that can be run independently.

Ignore failure of post build step in Jenkins

I have Jenkins job with summary plugin step in post build actions. Sometimes it fails because of absence of the necessary file for it(for some branches, and there is no ability to update them nearest time). Is it possible to ignore failure only in post build steps and mark build green?
Finally I found solution - in build steps add shell step with command "touch summaryName.xml". If file exists it won't be changed. And if there is no summary file it will be created
Yes, you can ignore the failure by ticking the check box [Do not fail this build if archiving returns nothing] in advanced section.

How can I add a Jenkins post action to execute two different scripts once the build successed or failed exclusively?

I want to execute a shell scripts if the Jenkins job build successful or another scripts if the Jenkins job build failed.
I added the post build task plugin, but it seems only can execute a shell in all status or just successful status, cannot specify another shell script to be run once build failed, the two scripts should be run exclusively.
are there anyone can help me on this?
use Post Build Task plugin for conditional steps
I use Conditional BuildStep Plugin and Text Finder Run Condition Plugin to execute steps when the build fails.
But there's a limitation with this method: You have to find a text that is displayed ONLY when your build fails.
See the pictures below:
I add exit 0 at the end of the script to force the build to be successful so that the program can execute the next step, which is Conditional steps(multiple).
You will see Execute script after a failed build. being displayed on console output when the job fails.
This is the execution result.
You will also need to find a text that is displayed ONLY when the first step succeeds so that you can trigger another script.

YSlow Phantomjs and Jenkins jobs failing, but analysis successful

I'm going through the tutorial on YSlow and Phantom js in Jenkins here: http://yslow.org/phantomjs/
Everything appears to be working great except the Jenkins builds are failing. I think this is due to the violations that YSlow is finding (6 for the particular site I am measuring). I'd rather have the build be successful (or unstable) vs. failed though
Is that possible with this or will I have to resort to something like the postgroovy or text finder plugin?
This is the console output:
phantomjs.exe yslow.js -i grade -t 50 --format junit http://www.somesite.com 1>yslow.xml
D:\Apps\Jenkins\workspace\YSlow_Test>exit 6
Build step 'Execute Windows batch command' marked build as failure
Thanks
Any non-zero exit code at the end of your Execute Windows batch command build step will result in build step being marked as failure.
To have the build step marked as success, you need an exit code of 0. I don't know anything about "yslow" or "phantomjs" and why they are giving you exit code of non-zero, but from "batch" side of things, you need only write exit 0 at the end of your build step if you want to overwrite the exit code of your phantomjs command.
You can then use Text Finder plugin to parse the console log and mark build as unstable when certain conditions are met.
Reading over this answer, Configuring yslow on Jenkins looks like you need TAP plugin to have the functionality of unit testing marking the build as unstable automatically

Is there information available when executing jenkins postbuild script about job status

I need to run certain shell command if build fails. Is there any variable passed to shell that contains such info? Or maybe I can find it in file?
I need something like that:
if [ $build_status == "FAIL" ]
then
do_the_magic
fi
I have already printed env and there is nothing that would directly say that build failed.
You are looking for the Conditional Build Step Plugin. Add a "Conditional step (single)" as your last build step and use "Current Build Status" for your condition. I like this plugin, but havent't used this particular condition yet.
Same solution explained step by step..Really easy, maybe not to elegant, but it works!
1: Catch all the build result you want to catch (in this case SUCCESS).
2: Inject an env variable valued with the job status
3: Do the Same for any kind of other status (in this case I catch from abort to unstable)
4: After you'll be able to use the value for whatever you wanna do.. in this case I'm passing it to an ANT script! (Or you can directly load it from ANT as Environment variable...)
Hope it can help!

Resources