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

6

22

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.

Alamo answered 2/9, 2011 at 15:40 Comment(0)
U
23

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>
...
Underbrush answered 24/8, 2012 at 13:11 Comment(3)
thanks. Also next step to get result as a single word would be: http://host/job/project/lastBuild/api/xml?depth=1&xpath=*/building/text(). It gives true or false.Safar
something interesting after testing cheking the color attribute if it has "_anime" on the name it seems works better =( i launch my jobs, and i test this 2 ways, using the last build "building" and say false when i use if there is anime on the color attribute return true, i am watching my job been executed, and building attribute return false, while the color return true, may i be doing something wrong? i will stick with the color for the mean time. thanks =) for the trick.Humanist
Looks like the <building> tag is available without specifying depth=1Thay
R
7

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.

Rectangle answered 23/7, 2014 at 15:7 Comment(0)
W
4

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
Wynnie answered 2/9, 2011 at 16:14 Comment(1)
I'm reasonably familiar with the API. Nothing on that call tells me explicitly that the build is running. I can figure it out because it tells me the "last build" was x, but the "last completed build" was x-1. But I'd really like an API that says "build running: yes", "running for: 00:01:17", "previous run time: 00:01:20", "scm revision: 14238", "changes: John - Fixed a bug in widget", etc.Alamo
F
2

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.

Flowery answered 2/9, 2011 at 18:8 Comment(1)
Thanks Gareth. Looking at the Javadoc for Job and seeing there was an isBuilding() method inspired me to look further through the source and find the information I was looking for at a marginally less obvious URL than I'd have expected (see edit to question).Alamo
E
0

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.

Encaenia answered 19/3, 2020 at 16:21 Comment(0)
M
0

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

Mouton answered 26/10, 2021 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.