I'm not sure if the summary is clear on this. What I want to do is create a jenkins build status badge url that will somehow be able to figure out the Git branch it is on, and add the correct branch suffix to the url dynamically.
I want to do this so I can create a readme file that always shows the master
, develop
, and <branch-i'm-looking-at>
jenkins builds of that repo, without having to manually update the badge for each branch.
I know how to create branch-specific build status badges. Jenkins automatically builds the markdown URL for you. For example one of mine for adevelop
branch looks like:
[![Build Status](https://jenkins.mycompany.com/path/to/build/develop/badge/icon?subject=develop)]
(https://jenkins.mycompany.com/path/to/build/develop/)
I can imagine it'd be something using the ${BRANCH_NAME}
jenkins env var, but i can't think of how to create it. I'm not experienced enough with git hooks or other scripting to even start to come up with a way to do it.
Has anyone done this?
Add a build badge to your readme.mdClick on the badge, then select the branch you want to report on. Paste the code into your readme.md . There are no more steps. You've successfully added a build badge.
Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.
Instead of creating a README
, I would maintain 4 README
s, one per branch, each one with their own static Jenkins URL, tailored to their own branch.
The README
in the develop
branch, for instance, would have the URL:
[![Build Status](https://jenkins.mycompany.com/path/to/build/develop/badge/icon?subject=develop)]
(https://jenkins.mycompany.com/path/to/build/develop/)
Also, these being multibranch pipelines, the branches will come and go.
Then I would consider a content filter driver.
That means only one README
file with a placeholder value in it.
("placeholder value": a string meant to be replaced. For instance: @URL@
)
That allows you to generate the right README
file locally, and to do so automatically on git clone
/git checkout
.
The generation script will:
README
to generate the right README
, with the proper "Build Status" URLFor that, do register (in a .gitattributes
declaration) a content filter driver.
(image from "Customizing Git - Git Attributes", from "Pro Git book")
The smudge
script will generate (automatically, on git checkout
or git switch
) the actual README
file as mentioned above.
That script can use:
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
See a complete example at "git smudge
/clean
filter between branches".
I would also add a clean
filter in order to restore the original README content (placeholder value), in order to not introduced (from a git diff
perspective) any difference.
So to recap:
git checkout
/restore
, the smudge
script declared in the .gitattributes
file is automatically activated, and replaces the placeholder value by the correct URL, with the branch namegit diff
/commit
, the clean
script declared in the .gitattributes
file is automatically activated, and replaces the generate URL by the original placeholder value.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With