Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BalakrishnaS1/a7f7a96b04500c83a3091695eaae1731 to your computer and use it in GitHub Desktop.
Save BalakrishnaS1/a7f7a96b04500c83a3091695eaae1731 to your computer and use it in GitHub Desktop.
import jenkins
import sys
import re
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)
server = jenkins.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)
#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)
for line in (consoleOutput.splitlines()):
if re.search("Finished| Loading pro| \[Failed\] | * tests executed ", line):
#print(line)
with open (Output_folder+"\\"+job_names+"_"+str(lastBuild)+".txt",'w') as f:
f.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment