Jenkinsfile clear gradle cache and retry on failure - jenkins

Current scenario: We have configured CI pipeline and running a jenkinsfile.groovy script for a gradle build. The pipeline is working fine and now we are planning to enable an error handling functionality into the groovy script.
Reason of this functionality: Sometimes, the gradle build is unable to download all the dependencies due to the network glitch or could be any reason. So, as a part of this functionality, we wants to add the following steps in the existing pipeline:
throw an error message of the failure
clear the cache from the gradle home directory
rebuild failed jenkins job
stop the existing failed build
Following code is included in the pipeline to enable the functionality:
catch (err) {
echo "Build failed for ${env.JOB_NAME}, clearing cache from the $GRADLE_HOME and retrying"
sh "rm -rf $GRADLE_HOME"
build "${env.JOB_NAME}"
throw err
}
Testing of new functionality: We are now in testing phase of the above code which is working fine, and three steps of the functionality is achieved, however, throw err (last step) doesn't stop the existing failed build after triggering same build again and keeps waiting for the build to complete.
Success of the new functionality in the existing pipeline:
throw an error message of the failure : ACHIEVED
clear the cache from the gradle home directory : ACHIEVED
rebuild failed jenkins job : ACHIEVED
stop the existing failed build : NOT ACHIEVED this fourth and the last step is not working as expected and doesn't stop the failed build. The failed build keeps waiting for the next job to complete. We have tried adding exit 1 as well but no luck.
Any help would be really appreciable. TIA

Related

How to trigger the failed build using Restart from stage without using UI

I want to trigger the previously failed build using its build number and Failed stage name in declarative pipeline without using UI
Say for Ex:
Build #1 Failed at Deploy stage
I don't want to build or test the Build #1 again
I catch the failed stage details using ${env.FAILED_STAGE}
Trigger build #2 as Build #1 as input parameter and the failed stage value
Here is the counterpart, I am not sure how to use Restart from stage option in Jenkins file
Could someone help me on this

How to stop a build by buildnumber in a Jenkins pipeline

We uses notes (comments) from Gitlab to issue a couple of user-commands, for example retry a build that seems to have failed from a failed integration etc. We would now like to ask jenkins to stop a build. We have got the buildnumber from the comment (through a callback to to gitlab searching the comments) so we know what build to stop but now we stumbled on the problem. We don't seem to find an api-call to stop the build by buildnumber alone.
We could of course make a request to https://server/project/buildnumber/stop (same url as used by the ui) but then we have to enable the crumbIssuer and according to Ops open for a CSRF-attack.
Is there a way to do this operation from inside a pipeline?
Navigate to Manage Jenkins > Script Console.
Run the following script setting the job name and number of the hung build accordingly:
def build = Jenkins.instance.getItemByFullName("jobName").getBuildByNumber(jobNumber)
build.doStop()
build.doKill()
Otherwise you can create a separate pipeline job and configure the above script
Abort the job build from a cid node (if the previous option did not help):
Log in to any cid node.
Run:
cd /srv/volumes/jenkins/jobs/<job-name>/builds/
rm -rf <hung-build-number>

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.

How to return "Not built" to Gerrit Trigger Plugin

There are 4 kind gerrit command in gerrit plugin settings.
Success / Fail / Unstable / Not Built.
I could return "success/fail" from my job to gerrit plugin, by using exit 0/1,
And "success/fail" gerrit command was executed.
But I don't Understand how to return "Not Built" by shellscript.
Please teach me.
sorry for my poor english.
Thanks.
Success / Failure / Unstable
If the job has no test reports configured:
If the last build command executed by Jenkins exits with failure
(<> 0) exit code, the build is considered to be failed.
If the last build command exits with success (= 0) exit
code, the build is successful.
If the job has one or more test reports configured:
If the last build command executed by Jenkins exits with failure
(<> 0) exit code, the build is considered to be failed.
If the last build command exits with success (= 0) exit
code but test reports have some failed tests, the build is unstable.
If the last build command exits with success and all test have
passed, the build is stable.
The only other way to get "Unstable" result is through the use of some post-build plugins (like static code analysis for example).
Not Built
I'm not sure about this item... but I think (guess) the build is considered not build when the job finishes by a timeout.
I found out way of force stopping by following command
curl http://127.0.0.1:8080/job/${JOB_NAME}/${BUILD_NUMBER}/stop
and gerrit command for "Not Build" was executed .

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

Resources