How to wait for job to finish when using jenkins-cli.jar to run a slave job? - jenkins

I have a Jenkins slave ( at localhost:8000 ) and I am executing it ( link ) from a Jenkins Master ( at localhost:8080 ). The basic idea here is to run the remote job and wait until the job is finished.
java -jar jenkins-cli.jar -s http://localhost:8000 build "Test Suite"
Right now, this doesn't wait. I starts the job on the slave and the Jenkins Master says the task is immediately finished although the slave runs for another 30 minutes.
Does anyone know how I can block or check for a signal on the slave to verify it is finished and get the exit status code of the job?
NOTE: My slave test MUST run on the slave because it won't run from Jenkins master, which runs as a service and doesn't allow permissions to start a webbrowser from the test. So, I run the slave in a visible console.

java -jar jenkins-cli.jar -s http://localhost:8000 build "Test Suite" -s
Adding the parameter -s to the build command should trigger the job and return after job is completed

Each windows application returns an exit code to it's parent. Examine the exit code using a windows batch file. Example:
java -jar jenkins-cli.jar -s http://127.0.0.1:9090 build "Test Suite" -s
echo The exit code is %errorlevel%
On successfull job completion you will get:
Completed "Test Suite" #72 : SUCCESS
The exit code is 0
Examine some error scenarios (wrong job name, job creates FAILED and so on)
Note: Running master jenkins not as windows service may be more easy.

Related

Jenkins job status failing despite running correctly

I have a jenkins jobs that executes with 'Publish over SSH'. The job connects to the remote server, transfers files and runs and ansible playbook.
The playbook runs as intended, confirmed by the logs. However at the end of the job an error is returned, failing the job. It's causing problems as it's preventing the pipeline from working correctly.
SSH: EXEC: completed after 402,593 ms
SSH: Disconnecting configuration [server] ...
ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [2]]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
[Run Playbook] $ /bin/sh -xe /tmp/jenkins1528195779014969962.sh
+ echo Finished
Finished
Finished: UNSTABLE
Is there a setting missing to allow this to pass?
never used the 'Publish over SSH' you are referingto, but I can recommend Jenkins Ansible Plugin. I am running several playbooks in pipeline stages here successfully from labeled build slaves (have one dedicated slave that has Ansible installed) targeting Linux hosts on cloud infrastructure via SSH.
Especially in combination with the ANSI color plugin the output very readable.
If you cannot try that plugin, check whats the return code of the playbook run shell call.

jenkins job does not finish even though it is successful

I have a Jenkins pipeline job in which I give a windows batch command to start my hybris server. Normally also if hybris is started from a command prompt, the server window (command window) will keep the process running and it does not exit. With the same thing happeining in Jenkins, the job never ends even though the server has successfully started. Pls suggest how I can get the server running, yet exit out of the job succesfully. Below is the pipeline script:
node {
stage 'ServerStop'
echo 'Stop the server if previously running'
bat '''call D:\\path_to_hybris\\hybris\\bin\\platform\\hybrisserver.bat stop
EXIT /B 0'''
stage 'Checkout'
echo 'Checkout related script'
stage 'Build'
echo 'Build script here'
stage 'ServerStart'
bat '''call D:\\path_to_hybris\\hybris\\bin\\platform\\start.bat
'''}

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

Cancelling Jenkins Job from Command line (CLI)

Just like 'build' command is used to start a job from Jenkins CLI, is there a way to cancel a long running job using CLI or any other command line technique. This I am trying in a windows machine and I am not looking to directly kill the process from task manager or shut down the jenkins.
Please suggest.
You can stop a Jenkins execution by calling JOB_URL/lastBuild/stop
Jenkins built-in Command Line tool does not provide a way to cancel/stop/abort a running build. But fortunately jenkins has provided other script API like Python API and Ruby API which have such ability.
Python API
class jenkinsapi.build.Build(url, buildno, job, depth=1)
stop()
Stops the build execution if it’s running :
return boolean True if succeded
False otherwise or the build is not running
API Link:https://jenkinsapi.readthedocs.org/en/latest/build.html
Ruby API
Class: JenkinsApi::Client::Job
#stop_build(job_name, build_number = 0) ⇒ Object (also: #stop, #abort)
Stops a running build of a job This method will stop the current/most recent build if no build number is specified.
API Link: http://www.rubydoc.info/gems/jenkins_api_client/1.4.2/JenkinsApi/Client/Job
This option assumes you have access to the Jenkins installation.
In Jenkins >2.195 you can do this using the Jenkins CLI jar:
# Export JENKINS_URL or add -s http://localhost[:port]/path/to/jenkins to the commands
export JENKINS_URL=http://localhost:8080/jenkins
# Get the name of the job you want to cancel
java -jar /path/to/jenkins-cli.jar -auth user:secret list-jobs
# Cancel the job
java -jar /path/to/jenkins-cli.jar -auth user:secret stop_builds <job_name>
# You can also disable the job, if needed
java -jar /path/to/jenkins-cli.jar -auth user:secret disable_job <job_name>

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.

Resources