简体   繁体   中英

How to get jenkins job build status using java

I'm trying to write a java program which will provide the build status of a particular job, this Jenkins job is running at every hour and I want to get the build status(success or failed) of each build for the last 24 hours. I can get the build information from the below url format : /job//api/json?tree=allBuilds[result,number,url,timestamp] OR http://jenkins.myteam-aws.local:8080/job/HealthCheckerApp/api/json?tree=allBuilds[result,number,url,timestamp] But the problem is when I logged in to the application I can retrieve the information but when I call the above url from the java application then I am getting the 500 error, possible cause of this error could be login credentials are missing. So how could I read this json data from the URL? Seeking your help in this regard.

// below code will read the json data from the given url

public class JsonReader {

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

public static void main(String[] args) throws IOException, JSONException {
    System.out.println("inside main-------->");
    JSONObject json = readJsonFromUrl("http://jenkins.myteam-aws.local:8080/job/HealthCheckerApp/api/json?tree=allBuilds[result,number,url,timestamp]");

    System.out.println("Json object created: " + json);
    System.out.println(json.toString());
}
}

Result: SUCCESS

Built No: 4715

url: http://jenkins.myteam-aws.local:8080/job/Thor-API-Prod-Health-Checker/4715/

Timestamp: 1545803471449

If you don't have access to Jenkins log and still think is a login problem, try with an username and an API Token .

You can get the API Token from http://<jenkins-server>/user/<username>/configure

And here you have two different ways to call with user and token to Jenkins. For example, a list of all jobs:

curl -X GET http://<jenkins-server>/api/json?pretty=true --user <username>:<api-token>

or an example for deleting a job

http://<username>:<api-token>@<jenkins-server>/job/<job-name>/doDelete

If you call URL.openConnection first and cast to HttpURLConnection then you can check the status and read the error text using getErrorStream() and throw an exception containing the error message.

Just replace the code for this method:

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    if (conn.getResponseCode() != 200) {
        // error occurred, get details and throw exception
        // this uses Commons IO to read all bytes from an InputStream
        byte[] errorBytes = IOUtils.readFully(conn.getErrorStream(), 2048, false);
        // if you have Java 9+ then use this instead:
        // byte[] errorBytes = conn.getErrorStream().readFully();
        String msg = new String(errorBytes, "UTF-8");
        throw new IOException(msg);
    }
    InputStream is = conn.getInputStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM