Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Last active January 5, 2024 08:37
Show Gist options
  • Save m-x-k/7c7e02de0abfb47b07d8e62aed93d00d to your computer and use it in GitHub Desktop.
Save m-x-k/7c7e02de0abfb47b07d8e62aed93d00d to your computer and use it in GitHub Desktop.
Python script to help get jenkins job console output
#!/usr/bin/env python
import re
import argparse
import requests
jenkins_url = "https://localhost:8080"
# Add new jobs here:
jobs = {
"my_job": "/folder/subfolder/jobname"
}
def call_jenkins(path):
return requests.get("{0}/{1}".format(jenkins_url, path), verify=False)
def build_jenkins_path(job):
paths = job.split("/")
path = "".join([p + "/job/" for p in paths])
return re.sub("/job/$", "",path)
def get_job_console_output(job):
path = build_jenkins_path(job)
url = "{0}{1}".format(path, "/lastBuild/consoleText")
return call_jenkins(url)
if __name__ == '__main__':
# Example: python jenkinsJobConsoleOutput.py -j my_job
parser = argparse.ArgumentParser()
parser.add_argument('-j', dest="job", help="Choose job to get console logs from")
args = parser.parse_args()
job_path = jobs.get(args.job)
result = get_job_console_output(job_path).text
print(result)
@BalakrishnaS1
Copy link

I sent the request from postman and got Status 200 Ok. I took the Authorization key from the header section and used it in my script. Now I am getting the below error:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment