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

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.

Related

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.

Restarting build steps from the failure point on Hudson/jenkins

My requirement is , Say for example for a job on Jenkins/Hudson , i have configured three build steps which perform 3 separate tasks.
Step1: Execute shell script (echo $PATH)
Step2: Execute Shell Script (sgsh)
Step3: Execute shell Script (echo $JAVA_HOME)
IN these build steps say for example step 2 fails , I need a plugin which will allow me to restart the build from step2 since step1 is already completed successfully. Is there anything available?
Check Conditional BuildStep Plugin . With Run Condition Plugin it do what you want.

Run Jenkins Build Step in Parallel

Is there a way to run Jenkins build step in parallel.
I know I can run multiple jobs in parallel, but I need to collate the unit test result and code coverage and use it generate a report.
So jobs in parallel might not be achieve the purpose, so I need to run build steps in parallel.
Any ideas?
Sounds like a job for the Jenkins Multijob plugin. You can create 2 phases:
Phase 1 : Builds jobs which run in parallel
Phase 2 : Collate results and generate report. You can specify this job to run only after the first phase is complete, and only if its successful etc.

How to run a conditional step in Jenkins only when a previous step fails

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.

running a shell script once build passes

I have a job that is running unit tests on a project build and then ssh into a staging server and pulling down from the master branch. Right now I'm using the post-build-script but this is running regardless of pass/fail. I'm trying to use the parameterized build plugin to trigger a new job when the build is passed. So far I've created the new job and set to trigger in the configuration of the original.
The new job is building ok on its own but the original job isn't triggering it. From 'Add post-build action' I've selected 'Trigger parameterized build on other projects' with build triggers:
Projects to build: new_job, Trigger when build is: Stable or unstable but not failed.
Any ideas appreciated!
C
If you don't actually need to pass a parameter to the second build, make sure that "Trigger build without parameters" is checked in the parameterized build trigger options.
The "Post build task" allows you to query the console log of the build step, and is executed only when criteria is met.
Jenkins writes BUILD SUCCESSFUL in the console log for every build step that had passed.
In your "Post build task" step, under Log text just put BUILD SUCCESSFUL, and under Script put your linux script/commands.
This way your script/commands will only be executed if the Build Step was successful

Resources