How to do a post-build task based off the exit code of the build script? - jenkins

I want to be able to read the exit code of the build script, from my post-build script.
Is there a way to do that in Jenkins configuration?
it will allow me to check for a matching string (in this case 'hella success rn') but it won't let me see the exit code of the build script?

You may use #!/bin/sh at the start of your Execute shell block as shown below:
Using #!/bin/sh without the default options that Jenkins uses i.e., #!/bin/sh -xe will prevent Jenkins from marking your Execute shell as failure. Doing this will help you run the subsequent command of finding the return status of your script.
-e Exit immediately if a command exits with a non-zero status.
Ref: http://linuxcommand.org/lc3_man_pages/seth.html
Based on the return status (script_status=$?) of your script, print something that you can search later in your post-build step.
Then, in the Post-build Actions, search that printed text to decide your post-build action as shown below:
Output:

Related

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.

Can Jenkins return 0 and 1 after test suites completed?

My query is related to Jenkins server.
I have made one API to hit the Jenkins server where Jenkins starts test suites.
My question is: can Jenkins server return 0 if any test case fail, and 1 otherwise?
The API URL is in the form
JENKINS_URL/job/Encore_Automation/build?token=TOKEN_NAME
By looking at Build Triggers / Trigger builds remotely (e.g., from scripts) it seems like this option only supports queuing a project and it does not let you retrieve results.
Jenkins REST API
After build has been triggered from REST API call, you could start making consecutive REST API calls to check it status.
Jenkins CLI
However Jenkins offers a jenkins-cli tool which let you not only to trigger the build but also to wait until its completion:
java -jar jenkins-cli.jar -s http://localhost:8080/ build JOB [-c] [-f] [-p] [-r N] [-s] [-v] [-w]
Starts a build, and optionally waits for a completion.
Aside from general scripting use, this command can be
used to invoke another job from within a build of one job.
With the -s option, this command changes the exit code based on
the outcome of the build (exit code 0 indicates a success)
and interrupting the command will interrupt the job.
With the -f option, this command changes the exit code based on
the outcome of the build (exit code 0 indicates a success)
however, unlike -s, interrupting the command will not interrupt
the job (exit code 125 indicates the command was interrupted).
With the -c option, a build will only run if there has been
an SCM change.
JOB : Name of the job to build
-c : Check for SCM changes before starting the build, and if there's no
change, exit without doing a build
-f : Follow the build progress. Like -s only interrupts are not passed
through to the build.
-p : Specify the build parameters in the key=value format.
-s : Wait until the completion/abortion of the command. Interrupts are passed
through to the build.
-v : Prints out the console output of the build. Use with -s
-w : Wait until the start of the command

In jenkins, execute shell command based on test result

I want to execute a shell command using
jenkins
based on test result. that means,
if the test result is successfull, i will execute the shell script and if not, i quit.
which plugin i have to use???
please save my searching time by giving ur suggestion.

How to stop a jenkins job midway?

I clicked on execute shell 3 times and have 3 shell scripts that jenkins runs during a build. How do I end a job mid build in one of the scripts?
I tried exit but that only ends the particular script and not the job.
If you click on the (?) help icon next to the "Execute Shell" field in your job configuration, you'll see that it says:
the build is considered a failure if any of the commands exits with a non-zero exit code.
Therefore, if you detect that you want to stop the build from a shell script, you can use exit 1 (to signify failure) rather than just calling exit, which is equivalent to exit 0 (which signifies success).
This will stop the build immediately, marking it as a failure — no other build steps will be executed.

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.

Resources