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

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> || :

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).

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

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.

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.

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