ssh error 127 with Jenkins - jenkins

I get the following console output while running a job on Jenkins:
Building in workspace /home/admin/.jenkins/jobs/ramdisk/workspace
[SSH] executing pre build script:
./build_script.sh
[SSH] exit-status: 127
[SSH] executing post build script:
[SSH] exit-status: 0
Finished: SUCCESS
When I run the above script manually on a terminal, it is fine.
Can you please guide what could be the issue here?

First of all, when you run script manually on the terminal, you could be getting exit status 127 as well, but since you are not checking the exit status on terminal, you are not noticing it.
Jenkins assumes that only exit code of 0 means success. Any other exit code means some failure. If your build_script.sh uses exit codes in non-standard way, it will make Jenkins think that it had failed.
Do the following directly on the machine:
./build_script.sh
echo $?
Make sure you do the echo command immediately after the script command, without doing anything else. This will print the exit code on the command line. Answer here what you get.
Second, open the build_script.sh in a text editor (vim, nano, whatever you use) and search for exit 127. If you find it, see what block does it exit from. If you got trouble reading the script, paste the content of the script (or just the block where you found exit 127) in here, and we could tell you why the script thinks it needs to exit.

Related

How do I tell Jenkins NOT to stop on fails, or put a condition on exit status?

I have a Jenkins shell script launching some python unittest followed by some cypress tests of my streamlit interface.
When all tests pass, everything works as intended, It generates nice html reports.
When any test fails, Jenkins finishes all tests but stops short of making the reports... What use are tests reports then ? ^^
Using
set -e
exit /b 0
works well when a test fails, but not when they all pass ! It then gives me an error
exit: Illegal number: /b
I also tried
set "errorlevel="
but this always marks the build as successful, even when a test failed...
So I tried this
if %ERRORLEVEL% gtr 0
then exit /b 0
else set "errorlevel="
fi
But %ERRORLEVEL% is not recognized, so it doesn't work...
Thanks for any help !

In Jenkins is possible to mark an aborted job as success?

In a job where i call terraform apply, after the deploy the shell is stuck and the job doesn't end so i have to abort it. Is it possible to mark the aborted job as success or there is another way to end this job without force?
On the job summary/artifacts/changes/triggers/results page you can "add a description". This is a good place to summarize results and explain what happened after job result is reviewed. The first part of description also appears in Build History.
Answer:
In shell scripts (or jenkins job script) you can use bash timeout command to protect any command that might get stuck. 'timeout [<option>] <duration> <command>'. e.g.
$ timeout 2s sleep 4
$ echo $?
124
You can check this exit value in script and exit with success or use the --preserve-status option to timeout to change exit code and have job considered as successful. Although if something is timing out it probably makes most sense to have the job marked as failed ?
Inside jenkins Execute shell you can wrap timeout with 'set -e' and 'set +e' so that the non-zero exit code will not be regarded as fail. Something like this would work for you:
set -e # no error if non-zero exit status
timeout <timeout> <terraform hanging command>
set +e
https://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html
Appended to the first answer, you have a set alternative just piping the semicolon. Nasty workaround in Bash, but works.
timeout <timeout> <terraform hanging command> || :

Jenkins and kill command in the script makes builds failed

Due some problems with the hanging of a python process (yandex-tank) during the build process in Jenkins (after which the build could not stop) i need to stop this problematic process with some additional kill command with timeout or using timeout command itself:
timeout $TIMEOUT yandex-tank-jmeter -i -o "jmeter.jmx=$WORKSPACE/$TEST_PLAN"
timeout sends default (15) kill signal, but after that the build goes to status FAILED.
Is there any workaround or special kill signal to make builds successful ?
Have you tried manual exit code overriding?
timeout $TIMEOUT yandex-tank-jmeter -i -o "jmeter.jmx=$WORKSPACE/$TEST_PLAN"; RES=$?
//If the command timed out, then RES equals 124.
...
//at the end of job scenario:
if [ $RES -eq 124 ]; then RES=0;
fi
exit $RES
According to the Jenkins documentation for the "Execute shell" step:
By default, the shell will be invoked with the "-ex" option.
Therefore, Jenkins places all shell code into a shell script file, in the temp directory, something like /tmp/sh/jenkins45723947385985.sh and then executes it as follows:
/bin/sh -xe /tmp/sh/jenkins45723947385985.sh
This can be seen in the console output of the job.
The e option in -xe means that the shell will exit as soon as it has an error. To change this behaviour add a custom shebang line to the start of the Jenkins shell script such as
#!/bin/sh -x
Jenkins will no longer terminate as soon as an error occurs.

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.

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