How do I check the Jenkins build status without switching to the browser?
If required, I can create a script using the JSON API, but I was wondering if there is already something like this built in.
How do I check the Jenkins build status without switching to the browser?
If required, I can create a script using the JSON API, but I was wondering if there is already something like this built in.
I couldn't find a built in tool so I made one:
Check to see if a build is running or not
I tried the Python script in the answer to this question, but couldn't get it to work. I don't know Python, and didn't want to invest any time in debugging, but was able to read enough of the script to gain inspiration from it.
All I need to do is check to see if a build is running or not. To do that I used curl and grep, like this:
curl http://myjenkins/job/myjob/lastBuild/api/json | grep --color result\":null
result\":null
will return 0.result\":null
will return 1.Not especially elegant, but it works well enough for my needs.
For example, I have a Bash script that starts a build, then waits for it to finish:
Thanks for the inspiration, Catskul!
A former colleague of mine wrote https://github.com/txels/autojenkins which has a whole bunch of convenience features and API type stuff around working with a Jenkins instance from Python...
Another Python solution:
I think I found an easier way. If I understood correctly, you want to check the result of the build - if it was a success or a failure, in other words.
Jenkins CLI's "build" command changes the exit code depending on the result of the build, as long as you use the
-s
or-f
option at the end.For example,
or
Notice that the option goes at the end; it's not the first
-s
, which is used to define the URL of the Jenkins instance.And then, to get the result, you can use
$?
:If the result is 0, it was a success. If it's something other than 0, it was a failure.
Reference: I can't find a public Jenkins instance that gives access to this page, but it can be found in your local Jenkins instance:
http://<url of Jenkins Instance>/cli/command/build
. It also explains the difference between-s
and-f
:You can use a Groovy script:
Via jenkins-cli
, where
=
means standard in. You can authenticate with--username <USER> --password <PASS>
or with-i <SSH-PRIVATE-KEY>
.Via jenkins-cli over SSH
Fortunately, there is a jenkins-cli that you can use to get some information from Jenkins. Unfortunately, you can't retrieve the status of a build using the CLI--which means your solution of using the JSON API is not only correct--it's the only programmatic way of doing so.
Also, while it looks like
get-job
might do what you want, it doesn't actually return the result--it only returns the job configuration.You can use the symbolic descriptor
lastBuild
:The
result
element in the response contains a string describing the outcome of the build.Another script for CMD (Windows):
You can try with this,