开发者

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

Is there an API to determine whether a given job is currently running or not?

Ideally, I'd als开发者_如何学Co 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.


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 go to your job's page, and add "api" to the end of the URL, you'll get information on using the API.

http://yourjenkins/job/job_name/api

More information on using the Jenkins API:

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API


If you're comfortable with digging through the Jenkins Java API, you could write a system Groovy script to get this data. The Job class is the place to start.


As stated on the /api page of your build (chapter "Accessing Progressive Console Output"), you can poll the console output with a GET request by calling <url-to-job>/lastBuild/logText/progressiveText. To quote the API doc:

If the response also contains the X-More-Data: true header, the server is indicating that the build is in progress

And there you go. You can test this behaviour by simply calling the respective URL in your browser and then inspecting the response headers with your browser's developer tools (usually accessed by pressing F12). In Firefox, the respective tab is called "network analysis" (assuming my translation is correct, my browser is not set to English). In Chrome, navigate to the "Network" tab.

This answer is based on Jenkins version 2.176.3.


It is also possible to look at the color attribute. I know it is not the wanted way. But maybe someone can make use of it. get the overview xml via "/job/api/xml" and then check the color attribute for "anim".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜