Determine if given job is currently running using Hudson/Jenkins API

dty picture dty · Sep 2, 2011 · Viewed 25.3k times · Source

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.

Answer

zageyiff picture zageyiff · Aug 24, 2012

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