How to stop a jenkins job midway? - jenkins

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.

Related

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

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:

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 .

Jenkins stop a build of specific job according to Parameter

How can I stop a build of specific job according to it's Parameter
for example:
I run the job FooJob multiple times with different FooParam with the following url:
http://10.10.10.10:8080/job/FooJob/buildWithParameters?FooParam=15
http://10.10.10.10:8080/job/FooJob/buildWithParameters?FooParam=19
http://10.10.10.10:8080/job/FooJob/buildWithParameters?FooParam=24
I am looking for url that can stop FooJob that has FooParam=19 if it's running
So I though if getting all the running builds of the job and check if it has the parameter FooParam=19 than to stop it
How can i do that?
Based on the platform (linux,window, other platforms). select build step within your Jenkins job .Execute shell or execute windows batch script. Just check parameter using simple "if condition" .If fooparam ==19 then stop your build by using "exit 0" (success) or "exit 1" (fail).

Cucumber test passed, but jenkins failed

I have a lots of tests on several server. I use Jenkins to manage all of them.
On one server (Slave Windows), when I launch a test in cmd, I got something like :
c:/tests/cucumber --tag #dev -p ie
...
1 scenarios (1 passed)
12 steps (12 passed)
0m31.761s
echo %errorlevel%
0
No error in the tests, and cucumber seems good.
When jenkins launch exactly the same tests, I get :
c:/jenkins_folder/cucumber --tag #dev -p ie
...
1 scenarios (1 passed)
12 steps (12 passed)
0m28.453s
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
The test passed, but is marked failed by jenkins.
The command "echo %errorlevel%" is aborted : the job fail before this point.
The same job played on another slave work.
Same problem with all profile and all tags.
Same problem when I replace profile by real value
I don't use the --strict flag
Jenkins, plugins : all up-to-date
Code of the windows batch :
cd /test8folder
cucumber --tag #dev -p ie
echo %errorlevel%
What did I missed ?
There doesn't seem to be anything wrong with the configuration.
The fact that you get Build step 'Execute Windows batch command' marked build as failure after your cucumber command without actually seeing the echo %errorlevel% executed only reaffirms that there was an error in cucumber (more on that later).
However, in Execute Windows Batch Command, even a command in error would not exit the batch script (unlike Jenkins's default Execute Shell implementation). You should be seeing at least some exit code, 0, 1, or anything.
The only time this would happen is if something within your buildstep executed exit /b [exit_code_num]. I don't know "cucumber", but if that is in-fact a cucubmer.bat and inside there is an exit /b statement, this is what's causing it drop out of the buildstep without continuing.
Solution
You can use call cucumber [whateverparams] so that even when it quits with exit /b, the control will return to the calling process, the Execute Windows Batch command script.
Try that first. And you will probably see that your echo %errorlevel% will probably return a non-zero value when executed under Jenkins, but at least you will see it now.
Now, as for why it succeeds on command prompt, but fails within Jenkins, there could be a lot of reasons, the most common one being environment variables and paths. We can tackle that later once we actually see the exit code of cucumber. You also said it worked on another node: even more reason to believe this is an environment issue, maybe a non-existent folder...
Edit:
The reason that even "successful" test execution exits the calling script is because exit /b 0 would still quit the calling script, even though cucumber exited with "success" 0

Resources