Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What URL will get the status code (result) of the last Jenkins job?

Tags:

jenkins

hudson

I am wondering if anyone knows what URL is required (as a GET or POST) that will get the status code (result) of the last Jenkins job (when the build# is not known by the client calling the GET request)? I just want to be able to detect if the result was RED or GREEN/BLUE .

I have this code sample, but I need to adjust it so that it works for Jenkins, for this purpose (as stated above):

public class Main {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost/jenkins/api/xml");
        Document dom = new SAXReader().read(url);
        for( Element job : (List<Element>)dom.getRootElement().elements("job")) {
            System.out.println(String.format("Name:%s\tStatus:%s",
                job.elementText("name"), job.elementText("color")));
        }
    }
}

Once I figure out the answer, I will share a full example of how I used it. I want to create a job that collects information on a test suite of 20+ jobs and reports on all of them with an email.

like image 381
djangofan Avatar asked May 08 '13 16:05

djangofan


People also ask

What is Jenkins job URL?

For a locally hosted Jenkins server, the URL would be: http://localhost:8080/env-vars.html. The easiest way to see how these Jenkins environment variables work is to create a freestyle job, echo each entry in the list and see the value Jenkins assigns to each property.

How do I find the latest build in Jenkins?

Navigate to the Jenkins dashboard and select Build History. This timeline displays your build history. This table displays a list of your past builds, time since build and the status of each build.

How do I get a list of jobs in Jenkins?

Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves: Jenkins. instance. getAllItems(AbstractItem.


1 Answers

You can use the symbolic descriptor lastBuild:

http://localhost/jenkins/job/<jobName>/lastBuild/api/xml

The result element contains a string describing the outcome of the build.

like image 103
Lars Kotthoff Avatar answered Oct 09 '22 01:10

Lars Kotthoff