Is there an API to determine whether a given job is currently running or not?
Ideally, I'd also like to be able to determine its estimated % complete and get the details of the SVN revision number and commit comment too!
EDIT:
I found the answer. http://host/job/project/lastBuild/api/
has almost all of what I need in it somewhere! If you kick off a manual build, it won't tell you the SCM changesets, but that makes sense. It does still tell you the latest SCM revision though, so that's good. All in all, good enough for my purposes right now.
The simplest way to access Jenkins REST API is to gather the User Token which is available by selecting your User and clicking on Configure. Now you will use the token as parameter for your authentication.
As gareth_bowles and Sagar said, using the Jenkins API is the way to know. If you put the depth to 1, you will see what you're looking for:
http://host/job/project/lastBuild/api/xml?depth=1
You will see there's a <building>
tag to tell if that build is running
... <build> <action> <cause> <shortDescription>Started by user Zageyiff</shortDescription> <userId>Zageyiff</userId> <userName>Zageyiff</userName> </cause> </action> <building>true</building> <duration>0</duration> <estimatedDuration>-1</estimatedDuration> <fullDisplayName>Project #12</fullDisplayName> <id>2012-08-24_08-58-45</id> <keepLog>false</keepLog> <number>12</number> <timestamp>123456789</timestamp> <url> http://host/job/project/12 </url> <builtOn>master</builtOn> <changeSet/> <mavenVersionUsed>3.0.3</mavenVersionUsed> </build> ...
I'm using the Groovy plug-in, and run the following snippet as system:
import hudson.model.* def version = build.buildVariableResolver.resolve("VERSION") println "VERSION=$version" def nextJobName = 'MY_NEXT_JOB' def nextJob = Hudson.instance.getItem(nextJobName) def running = nextJob.lastBuild.building if (running) { println "${nextJobName} is already running. Not launching" } else { println "${nextJobName} is not running. Launching..." def params = [ new StringParameterValue('VERSION', version) ] nextJob.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params)) }
It works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With