如何从 Jenkins 获取当前分支?
How to fetch the current branch from Jenkins?
我想使用它的 API 和 Python 查询 Jenkins 以获取当前准备构建的分支。
我该怎么做?
从 jenkins API 你可以查看
lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]
也许你可以做的是构建你的东西,并在你的构建工作完成后触发第二个工作。
然后在这个新工作中,你可以找到分支名称
我不使用 python,但是使用 jq 你可以像这样在数组中获取分支名称:
jq -r '.actions[].buildsByBranchName | select(. != null)'
完整代码(您当然可以正确替换 bash 变量):
JENKINS_API_URL=$JENKINS_SERVER/job/$DEPLOY_JOB/lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]
BRANCHES_JSON=$(curl --globoff --insecure --silent $JENKINS_API_URL)
BRANCHES=`echo $BRANCHES_JSON | /var/lib/jenkins/tools/jq/jq -r '.actions[].buildsByBranchName | select(. != null)'`
我终于做到了。
# 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
我想使用它的 API 和 Python 查询 Jenkins 以获取当前准备构建的分支。
我该怎么做?
从 jenkins API 你可以查看
lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]
也许你可以做的是构建你的东西,并在你的构建工作完成后触发第二个工作。
然后在这个新工作中,你可以找到分支名称
我不使用 python,但是使用 jq 你可以像这样在数组中获取分支名称:
jq -r '.actions[].buildsByBranchName | select(. != null)'
完整代码(您当然可以正确替换 bash 变量):
JENKINS_API_URL=$JENKINS_SERVER/job/$DEPLOY_JOB/lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]
BRANCHES_JSON=$(curl --globoff --insecure --silent $JENKINS_API_URL)
BRANCHES=`echo $BRANCHES_JSON | /var/lib/jenkins/tools/jq/jq -r '.actions[].buildsByBranchName | select(. != null)'`
我终于做到了。
# 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