如何从詹金斯获取当前分支? [英] How to fetch the current branch from Jenkins?
本文介绍了如何从詹金斯获取当前分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用它的API和Python查询Jenkins,以获取当前准备构建的分支.
I would like to query Jenkins using it's API and Python to fetch the branch that is currently ready to be built.
我该怎么做?
推荐答案
我终于做到了.
# 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
这篇关于如何从詹金斯获取当前分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文