Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a job's build status as post-build variable

Is there a way to obtain the status of the Jenkins job in a variable during a Post-Build shell script?

I want to print out the message Build Status is $BUILD_URL :: $BUILD_STATUS, where $BUILD_STATUS is the status of the current completed build (e.g. ABORTED, SUCCESS, or FAILURE).

like image 323
Jose Avatar asked Mar 08 '14 03:03

Jose


2 Answers

If you can invoke a python script as a post-build step, you can try something like this:

import os, sys, json, codecs, urllib2

def main():
    url = "http://localhost:8080/job/jobName/lastBuild/api/json"
    try:
        fRead = urllib2.urlopen(url, None, 30); # 30 second timeout
    except:
        raise
    jsonResponse = json.loads(fRead.read());
    fRead.close();
    jobStatus = jsonResponse["result"]

main();

I have tested the url on my Jenkins and it works, but I haven't tested the script itself, so be wary. Obviously, substitute the port number and jobName as appropriate.

like image 106
user3352495 Avatar answered Sep 23 '22 06:09

user3352495


In my case, I had to include the API TOKEN here is what worked for me:

BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi/json | jq -r '.result')

which for me was:

BUILD_STATUS=$(curl --user robert:valueofmysecrettoken --silent $BUILD_URLapi/json | jq -r '.result')
like image 34
Robert Misior Avatar answered Sep 23 '22 06:09

Robert Misior