Skip to content

Instantly share code, notes, and snippets.

@carsongee
Last active February 15, 2021 14:04
Show Gist options
  • Save carsongee/0ed0613eaa2d26eed437 to your computer and use it in GitHub Desktop.
Save carsongee/0ed0613eaa2d26eed437 to your computer and use it in GitHub Desktop.
Tail Jenkins Jobs
#!/usr/bin/env python
"""
Tail a Jenkins build in a terminal
"""
import os
import requests
import sys
import time
JENKINS_BASE = "https://example.com"
JENKINS_JOB = "build-software"
s = requests.Session()
s.auth = (
os.environ.get('JENKINS_USERNAME', None),
os.environ.get('JENKINS_PASSWORD', None)
)
start = 0
cont = True
while cont:
response = s.get(
'{base}/{job}/{job_number}'
'/logText/progressiveText?start={start}'.format(
base=JENKINS_BASE, job=JENKINS_JOB,
job_number=sys.argv[1], start=start
)
)
if response.status_code != 200:
print('Job complete or not found')
sys.exit(1)
if start != response.headers['X-Text-Size']:
print(response.text)
start = response.headers['X-Text-Size']
try:
cont = response.headers['X-More-Data']
except KeyError:
sys.exit(0)
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment