Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 416
dty Avatar asked Sep 02 '11 15:09

dty


People also ask

How do I access REST API Jenkins?

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.


2 Answers

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> ... 
like image 117
zageyiff Avatar answered Sep 23 '22 03:09

zageyiff


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.

like image 30
Damien Coraboeuf Avatar answered Sep 24 '22 03:09

Damien Coraboeuf