Python 如何从Jenkins获取当前分支?
我想使用它的API和Python查询Jenkins,以获取当前准备构建的分支Python 如何从Jenkins获取当前分支?,python,jenkins,Python,Jenkins,我想使用它的API和Python查询Jenkins,以获取当前准备构建的分支 我该怎么做呢?从jenkins API可以查看 lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName] 也许你能做的就是构建你的东西,然后在构建工作完成后触发第二个工作 然后在这个新作业中,您可以找到分支名称 我不使用python,但使用jq可以在如下数组中获得分支名称: jq -r '.actions[].buildsByBranchName |
我该怎么做呢?从jenkins API可以查看
lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]
也许你能做的就是构建你的东西,然后在构建工作完成后触发第二个工作
然后在这个新作业中,您可以找到分支名称
我不使用python,但使用jq可以在如下数组中获得分支名称:
jq -r '.actions[].buildsByBranchName | select(. != null)'
完整代码(当然可以正确替换bash变量):
我终于做到了
# 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
#jenkins config.xml的url
詹金斯http://11.11.111.11:8686/job/TheJob/config.xml'
j_user=“某人”
j_pass=“somepass”
def get_jenkins_分支机构名称(jenkins_url、j_用户、j_通行证):
"""
函数转到提供的jenkins XML url,
与经过身份验证的用户进行身份验证,
抓住xml,
把它翻成字典,
在字典中搜索分支名称
"""
导入请求,xmltodict
从requests.auth导入HTTPBasicAuth
#使用经过身份验证的用户获取url
response=requests.get(jenkins_url,auth=HTTPBasicAuth(j_user,j_pass))#响应必须是200
#响应的内容是xml
xml=response.content
#将xml解析为字典
jenkins_dict=xmltodict.parse(xml)
#获取实际的分支名称
branch_name=jenkins_dict['project']['scm']['branchs']['hudson.plugins.git.BranchSpec']['name']
返回分支机构名称
您如何定义“当前准备构建的分支”?嗯。。。詹金斯API对此怎么说?你看了吗?投票失败的原因:没有个人努力。:]我确实搜索了很多。据我所知,Jenkins没有官方API。我在python中找到了这些实现。然而,在它们中我都找不到我想要的东西。有一个官方的詹金斯api。只需将
/api
添加到任何Jenkins URL即可查看文档。例如:http://example.com:8080/api
# 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