9

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?

Max Cascone
  • 648
  • 9
  • 25

1 Answers1

6

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.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for this, but I don’t think this is an answer. As I state in the original question, I already know how to create branch-specific build status badges. Jenkins automatically builds the markdown URL for you. Maintaining multiple readmes is not a scalable solution. Also, these being multibranch pipelines, the branches will come and go. Only `develop` and `master` are static. I need a _dynamic_ solution. – Max Cascone Dec 29 '20 at 19:44
  • @MaxCascone A dynamic solution? Sure thing. I have edited the answer to proposes a possible implementation. – VonC Dec 30 '20 at 00:14
  • Now THIS looks extremely interesting. Looking forward to trying it and will get back ASAP. – Max Cascone Dec 30 '20 at 15:34
  • I finally read through this thoroughly, and I realize it's not the answer. And that's my fault for not explaining the issue correctly. What I need is, something that will show _in the GitHub web UI_ the branch name in the URL, without committing unique files per branch. Is that more clear? – Max Cascone Nov 16 '21 at 18:28
  • @MaxCascone Clearer indeed, but not as easily achieved than creating a readme file (as mentioned in the original question). I don't have an immediate solution, but will keep looking. – VonC Nov 16 '21 at 22:27
  • ...sorry it took me a year to look this closely. I appreciate the quick response! – Max Cascone Nov 17 '21 at 04:08