Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make jenkins build status badge dynamically show the branch of the its file is in

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?

like image 229
Max Cascone Avatar asked Oct 15 '20 15:10

Max Cascone


People also ask

How do you add a build status in readme?

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.

How do I parameterize a branch in Jenkins?

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.


1 Answers

Instead of creating a README, I would maintain 4 READMEs, 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:

  • detect the current checked-out branch
  • replace the placeholder value in README to generate the right README, with the proper "Build Status" URL

For that, do register (in a .gitattributes declaration) a content filter driver.

smudge (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.

https://git-scm.com/book/en/v2/images/clean.png

So to recap:

  • on 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 name
  • on git diff/commit, the clean script declared in the .gitattributes file is automatically activated, and replaces the generate URL by the original placeholder value.
like image 185
VonC Avatar answered Oct 29 '22 05:10

VonC