Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Last active August 29, 2015 14:21
Get the branch name that is currently in jenkins
__author__ = 'wehappyfew'
# the url of jenkins config.xml
jenkins_url = 'http://11.11.111.11:8686/job/TheJob/config.xml'
j_user = "someone"
j_pass = "somepass"
def get_jenkins_branch_name(jenkins_url, j_user, j_pass):
"""
The function goes to the provided jenkins XML url,
authenticates with an authenticated user,
grabs the xml,
turns it to dictionary,
searches inside the dictionary for the branch name
"""
import requests,xmltodict
from requests.auth import HTTPBasicAuth
# get the url with an authenticated user
response = requests.get(jenkins_url, auth=HTTPBasicAuth(j_user, j_pass)) #the response must be 200
# the content of the response is the xml
xml = response.content
# parse the xml to a dictionary
jenkins_dict = xmltodict.parse(xml)
# grab the actual branch name
branch_name = jenkins_dict['project']['scm']['branches']['hudson.plugins.git.BranchSpec']['name']
return branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment