Skip to content

Instantly share code, notes, and snippets.

@BalakrishnaS1
Created May 5, 2020 16:44
Show Gist options
  • Save BalakrishnaS1/1095a853d6e7c07e1212aad8550aaf07 to your computer and use it in GitHub Desktop.
Save BalakrishnaS1/1095a853d6e7c07e1212aad8550aaf07 to your computer and use it in GitHub Desktop.
Reads jenkins job console output and extract desired patterns to a file
from jenkins import *
from jenkinsapi.utils.requester import Requester
import sys
import re
import os
from getpass import getpass
#Usage of this script:python Jenkins_Console.py "http://localhost:8081/job/00_OPS/" "C:\Users\balakrishna\Desktop\ExtractedLogs"
#Base url where all our jobs are present
#url = 'http://localhost:8081/job/00_OPS/'
base_url = sys.argv[1]
print("Base url where all our jobs are present", base_url)
#Credentials
username = input("Please enter username:")
password = getpass("Please enter password:")
#Folder where we copy our extracted logs
Output_folder = sys.argv[2]
print("Path of output folder where all our extracted logs are present", Output_folder)
os.environ.setdefault("PYTHONHTTPSVERIFY", "0")
server = Jenkins(base_url, username= username, password= password)
# Get all builds, this give us the list of jobs
jobs = server.get_all_jobs(folder_depth=None)
pattern = ['Loading project resources', '[Failed]', 'tests executed','ForceDeploy=','Started by user']
#Printing all the jobs using this for loop and fullname is used to filter the job name
for job in jobs:
job_names=(job['fullname'])
#print("job_names------->",job_names)
#Get the information on the last build of a particular job_name using lastBuild and number
info = server.get_job_info(job_names)
lastBuild = info['lastBuild']['number']
print("The job name is:",job_names,"and their last build is:",lastBuild)
#Get the console Output using the below
consoleOutput = server.get_build_console_output(job_names, lastBuild)
#print (consoleOutput)
l=[]
for i in consoleOutput.splitlines():
#print (i)
for j in pattern:
if j in i:
l.append(i)
with open (Output_folder+"\\"+job_names+"_"+str(lastBuild)+".txt",'w') as f:
for i in l:
f.write(i+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment