How to fetch the current branch from Jenkins?

2024/11/14 13:14:07

I would like to query Jenkins using it's API and Python to fetch the branch that is currently ready to be built.

How can I do that?

Answer

From the jenkins API you can check

lastSuccessfulBuild/api/json?tree=actions[buildsByBranchName]

Maybe what you can do is build your stuff, and have a second job triggered after your build job is finished.

Then in this new job, you can find the branch name

I dont use python, but with jq you can get the branch names in an array like this:

 jq -r '.actions[].buildsByBranchName | select(. != null)'

The full code (you can of course replace the bash vars correctly):

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)'`
https://en.xdnf.cn/q/119471.html

Related Q&A

How to vertically stretch graphs with matplotlib subplot [duplicate]

This question already has answers here:How do I change the size of figures drawn with Matplotlib?(16 answers)Closed 5 years ago.With the following code, I try to plot 12 different histograms in one pi…

Python Selenium Traceback (most recent call last):

Im trying to use selenium for a python web scraper but when I try to run the program I get the following error: "/Applications/Python 3.8/IDLE.app/Contents/MacOS/Python" "/Applications/P…

getting error while installing opencv via pip

python version = Python 3.8.0pip version = 19.3.1C:\Users\Sami Ullah Ch>pip3 install opencv-pythonERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)

Check whether text contains x numbers in a row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

How to add polling interval for a GET request in python

I have a case where I have to keep checking the response of a GET call until I see the status as success in the api response. And it takes around 20 to 50 mins to get the status from active to success.…

Total beginner wrote a tic tac toe game in Python and would like some feedback

Ive decided to learn Python about 2 weeks ago, been going through various books and videos, and Ive decided to try my hand at programming a tic tac toe game. I was somewhat successful (it doesnt recogn…

Separating tag attributes as a dictionary

My entry (The variable is of string type): <a href="https://wikipedia.org/" rel="nofollow ugc">wiki</a>My expected output: { href: https://wikipedia.org/, rel: nofollow …

How can I remove duplication of 2 separate which is interrelated with each other (PYTHON)

After reading so many title, I couldnt solved the problem below. Does anyone can help me please ? For instance, I have 2 list (list_I and list_II) which is interrelated with each other. list_I = [123,…

Array within an array?

Im trying to call up an element from an array within an array in Python. For example:array = [[a1,a2,a3,a4], [b1,b2,b3,b4], [c1,c2,c3,c4]]The question is, how would I print just the value b1?