Max Cascone
Max Cascone

Reputation: 833

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?

Upvotes: 9

Views: 5263

Answers (1)

VonC
VonC

Reputation: 1323175

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.

Upvotes: 7

Related Questions