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.
Asked
Active
Viewed 1.1k times
2
-
I think that with an exit on error in batch script should stop the job. – 23ars Nov 27 '15 at 09:54
-
I didn't get it. Can you please give me an example? By the way, I am running the job as a background process. – Affi Nov 27 '15 at 09:56
-
exit /b
is for exit a script/command. this should stop the jenkins job that runs the script. – 23ars Nov 27 '15 at 10:00 -
This doesn't help. As I am starting the job once from cmd, the job is started in jenkins and this command is not impacting the jenkins job which continues running. I hope there should be cli command to hit the jenkins job for stopping – Affi Nov 27 '15 at 13:08
3 Answers
0
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

mainframer
- 20,411
- 12
- 49
- 68
0
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>

Jonathan Agosto
- 1
- 2