Viewing Jenkins Status From the Command Line via the Jenkins API
This is a python script that I created to print out the status of a set of Jenkins jobs in color on the command line. Right now the coloring for this script works on windows only.
defset_text_color(color):#This method works on windows onlycolor_codes={"BLUE":0x0001,"GREEN":0x0002,"RED":0x0004,"MAGENTA":0x0005,"YELLOW":0x0006,"GRAY":0x0007}STD_OUTPUT_HANDLE=-11fromctypesimportwindll,Structure,c_short,c_ushort,byrefstdout_handle=windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)SetConsoleTextAttribute=windll.kernel32.SetConsoleTextAttributeSetConsoleTextAttribute(stdout_handle,color_codes[color])defprint_build_status(jenkins_url):importjson,urllib.requestrequest=urllib.request.urlopen(jenkins_url)encoding=request.headers.get_content_charset()data=json.loads(request.read().decode(encoding))forjobindata['jobs']:if'anime'injob['color']:job['name']=job['name']+' Running...'if'blue'injob['color']:set_text_color("GREEN")print("+ "+job['name'])elif'yellow'injob['color']:set_text_color("YELLOW")print("- "+job['name'])elif'red'injob['color']:set_text_color("RED")print("- "+job['name'])elif'aborted'injob['color']:set_text_color("GRAY")print("- "+job['name'])else:set_text_color("MAGENTA")print("- "+job['name'])importsysprint_build_status(sys.argv[1])
Here is an example showing the status of a set of jobs from the Jenkins public CI server:
Normally I use this script with an alias so that I can just type something like “jobs” and see a list of the Jenkins jobs that I care about. You can replace “http://your-jenkins-url/api/json” with any Jenkins api resource that lists Jenkins Jobs. For example, I have two custom views, one for projects being built and one for test jobs that I am interested in. I use this script so that I can quickly see from the command line the status of those Jenkins jobs without switching context to a web browser.