Jenkins execute command only if previous one failed - jenkins

I have the following problem. I have a command that unfortunately only works from time to time in powershell. Sometimes an error comes sometimes it runs through.
Now I am looking for an option in the declarative Jenkins pipeline to execute a step, if this step fails it should execute another command.
However, if the first command runs through, skip the second command, because it is then no longer necessary.
Unfortunately I don't know at all how to implement this.
I have thought about catch error.
I have thought about if else .

Maybe what you need it's already resolved here. I've been working with try/catch because I had same issues of unstable executions and works like a charm.
Try-catch block in Jenkins pipeline script

Related

Declarative Jenkinsfile doens't exit even if shell-script return non-zero value

Consider this extract from my declarative syntax Jenkinsfile
stage("Test") {
steps {
sh "sh run-tests.sh"
}
}
Even though the run-tests.sh script exit with code 1, the Jenkins job execution continues to the next step. Does anyone know what may be causing this?
EDIT: Thanks for the replies so far. I believe the problem lies elsewhere - the tests being executes are initiated by Python's nose2 library, and if I'm not mistaking it's this command that exits with 0 regardless of the status of the tests. I'll follow that lead for now and see if that solves things.
I found the solution. I'm running the tests using docker-compose, and it turns out that docker-compose return 0 regardless of what my test script return. See this SO post for more info.
I added the --exit-code-from option to my docker-compose up command, and now my CI job aborts with the same exit code as my Jenkinsfile (and thus my test script).

Use IDE to generate pipline script for jenkins

I need to overload jenkins functions to debug pipeline script in IDE.
I'm new to java/groovy etc. I'm going to write a several hundred lines scripted pipeline. Groovy is based on java. As I'm new I prefer the function name completion/suggestion and a debugging feature would be awesome where I can walk through the lines step by step and see what is in the vars.
I set up a eclipse Luna with the groovy-plugin. Which is actually working =)! (for newest eclipse the plugin is not yet ready). Also debugging is quiet cool!
But special jenkins expressions will still throw errors.
node(MasterName){ ... }
sh
...
Is there a chance to overload those functions?
Just killing the error, not performing any actions. Maybe converting it to a print like "I'm executing script XYZ" or "Switching to node BLUBB"?
The outcome should be a copy paste script, for checking in and running with jenkins without major changes.
Is there any better way?
In the end it turned out, that even if you omit the pipeline specific expressions, you will still have trouble. In some more complex situations e.g. calling a constructor with super in a extended class and executing a function afterwards, it does not behave the same then in my local python interpreter.
So what I did was an error to assume, my Ubuntu system default groovy interpreter would work the same like the jenkins interpreter. It would be nice to run a debugger inside the jenkins environment, or going step by step through the pipeline script and see how it is actually working without a print in every second line.

How can I use Groovy Postbuild to force a job to success?

I am trying to force a unit test job to success if the main build returned a failed exit code. Here is how I configured it and it is not working (see screenshot). Does anyone know any reason why this shouldn't work? In this example I just want to show that a failing job can be changed to a passing job by a groovy postbuild step. The plugin doc implies that this should work.
The main build runs a batch script with "EXIT 1" to fail the build.
A Groovy Postbuild step runs manager.buildSuccess() in an attempt to force the build to success (but it fails to do so with no error).
I found a discussion about that problem on jenkins-JIRA.
As a workaround it is proposed to use reflection to make the build successful:
manager.build.#result = hudson.model.Result.SUCCESS
This workaround doesn't work for me, but perhaps it can help you
Install Groovy Post Build Plug in.
Then add the below Groovy Script.
manager.build.#result = hudson.model.Result.SUCCESS
Uncheck the Use Groovy Sandbox. This worked for me.

Why Jenkins is failing after executing an external windows command line?

I have to create a job in my local Jenkins installation where it executes a SonarQube analysis and then calls a command-line program which searches for duplicated lines of code. However, when I execute the latter command (cpd), it runs okay since it outputs correctly in a external file, but Jenkins still points out it as an error like this:
E:\BASE_DIR>cpd --minimum-tokens 100 --files "E:\BASE_DIR\Project_Folder" --language java 1>>"E:\BASE_DIR\Project_Folder\CPD_CLIG.txt"
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I've tried to create another script which calls that command but I've got the same result.
Any suggestions on how to handle this? Any work-around would be very helpful.
Simple and short answer to your question is
Please add following line into your "Execute shell" Build step.
"#!/bin/sh"
Now let me explain you the reason why we require this line for "Execute Shell" build job.
By default Jenkins take "/bin/sh -xe" and this means -x will print each and every command.And the other option -e, which causes shell to stop running a script immediately when any command exits with non-zero(when any command fails) exit code.
So by adding the "#!/bin/sh" will allow you to execute with no option.
please refer : How/When does Execute Shell mark a build as failure in Jenkins?
I didn't find a proper solution for my issue but I realized that I can use a Jenkins plugin for static code analysis (DRY Plugin and PMD Plugin) to adress my problem.
Thanks you all.

Jenkins - Make pass/fail dependent on results from script command

I'm admittedly very new to using Jenkins, so I apologize if this is something simple I'm overlooking. In my Jenkins job, I have a bash command to run a Python script. Everything runs correctly at the moment, and the Python script works. However, the script can give a pass or fail result after running (to clarify, a fail doesn't mean the script crashed, just that it ran through and, with the variables given, gave the result that with those variables it is wrong). I need to make it so the job fails when the "fail" result is given, but I can't figure out a way to make the Jenkins pass/fail dependent on anything other than whether everything runs properly. How can I set it in such a way that whether the job passes or fails depends on the python script output? Thanks in advance for your help!
So from what I could figure out, the easiest way to do this is to modify the Python script so that it exits with a non-zero value when it returns the undesirable value, and with zero otherwise, so the success of the job will mimic the success of the script.

Resources