114

In my github repo's readme.md file I have a Travis-CI badge. I use the following link:

https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging

The obvious problem is that the branch is hardcoded. Is it possible to use some sort of variable so that the branch is the one currently being viewed?

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Joe Gatt
  • 2,197
  • 3
  • 15
  • 19
  • 3
    https://github.com/github/markup/issues/913 – Shanimal Nov 16 '18 at 20:21
  • 3
    It should also be possible to make the repository part a variable also, so that forked repositories don't incorrectly report the status of the original repository they are forked from. – EoghanM May 27 '20 at 15:23

4 Answers4

57

Not that I know of.
GitHub support confirms (through OP Joe Gatt 's comment)

The only way to do this would be to pass the link through my own service which would use the github's http referrer header to determine which branch is being referenced and then fetch the appropriate image from Travis CI

I would rather make one Travis-CI badge per branch, for the reader to choose or consider the appropriate when seeing the README.md.


Update 2016 (3 years later): while nothing has changed on the GitHub side, fedorqui reports in the workaround mentioned in "Get Travis Shield on Github to Reflect Selected Branch Status" by Andrie.
Simply display all the branches and their respective TravisCI badges.

If you have only two or three branches, that could be enough.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thanks, VonC. I contacted github support, as you suggested and here's their reply: – Joe Gatt Sep 08 '13 at 13:55
  • 1
    They confirmed that this can't be done. They said the only way to do this would be to pass the link through my own service which would use the github's http referrer header to determine which branch is being referenced and then fetch the appropriate image from Travis CI... – Joe Gatt Sep 08 '13 at 14:01
  • @JoeGatt good feedback. I have included that conclusion in the answer for more visibility. – VonC Sep 08 '13 at 15:56
  • 4
    I just tried to pass the link through my own service but unfortunately I don’t get the `HTTP_REFERER` when the image is loaded from the README on GitHub. :-( – 0xced Mar 19 '14 at 22:47
  • 3
    Well, I guess it’s now impossible to achieve because of [SSL Proxied Assets](https://github.com/blog/743-sidejack-prevention-phase-3-ssl-proxied-assets). – 0xced Mar 19 '14 at 23:56
  • Are you aware of any change on this? I mean, is it still not available? – fedorqui Sep 02 '16 at 11:28
  • 2
    @fedorqui As far as I know, it is not available. – VonC Sep 02 '16 at 11:29
  • Thanks a lot for the feedback. I found an interesting answer [using `?branch=...`](http://stackoverflow.com/a/32762503/1983854) that Travis can handle internally. – fedorqui Sep 02 '16 at 11:33
  • @fedorqui Yes, that is what this question uses as well: but you can not pass a variable for that branch name, can you? – VonC Sep 02 '16 at 11:37
  • No, no that I am aware of :/ – fedorqui Sep 02 '16 at 11:38
  • 1
    @fedorqui Still, I have included your comment in the answer for more visibility. – VonC Sep 02 '16 at 11:40
21

I worked around this issue with a git pre-commit hook that re-writes the Travis line in the README.md with the current branch. An example of usage and pre-commit (Python) code (for the question as asked) are below.

Usage

dandye$ git checkout -b feature123 origin/master
Branch feature123 set up to track remote branch master from origin.
Switched to a new branch 'feature123'
dandye$ echo "* Feature123" >> README.md 
dandye$ git add README.md 
dandye$ git commit -m "Added Feature123"
Starting pre-commit hook...
Replacing:
    [![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=master)][travis]

with:
    [![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][travis]

pre-commit hook complete.
[feature123 54897ee] Added Feature123
 1 file changed, 2 insertions(+), 1 deletion(-)
dandye$ cat README.md |grep "Build Status"
[![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][travis]
dandye$ 

Python code for the pre-commit code

dandye$ cat .git/hooks/pre-commit
#!/usr/bin/python
"""
Referencing current branch in github readme.md[1]

This pre-commit hook[2] updates the README.md file's
Travis badge with the current branch. Gist at[4].

[1] http://stackoverflow.com/questions/18673694/referencing-current-branch-in-github-readme-md
[2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
[3] https://docs.travis-ci.com/user/status-images/
[4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9
"""
import subprocess

# Hard-Coded for your repo (ToDo: get from remote?)
GITHUB_USER="joegattnet"
REPO="joegattnet_v3"

print "Starting pre-commit hook..."

BRANCH=subprocess.check_output(["git",
                                "rev-parse",
                                "--abbrev-ref",
                                "HEAD"]).strip()

# String with hard-coded values
# See Embedding Status Images[3] for alternate formats (private repos, svg, etc)

#  [![Build Status](https://travis-ci.org/
#  joegattnet/joegattnet_v3.png?
#  branch=staging)][travis]

# Output String with Variable substitution
travis="[![Build Status](https://travis-ci.org/" \
       "{GITHUB_USER}/{REPO}.png?" \
       "branch={BRANCH})][travis]\n".format(BRANCH=BRANCH,
                                            GITHUB_USER=GITHUB_USER,
                                            REPO=REPO)

sentinel_str="[![Build Status]"

readmelines=open("README.md").readlines()
with open("README.md", "w") as fh:
    for aline in readmelines:
        if sentinel_str in aline and travis != aline:
            print "Replacing:\n\t{aline}\nwith:\n\t{travis}".format(
                   aline=aline,
                   travis=travis)
            fh.write(travis)
        else:
            fh.write(aline)

subprocess.check_output(["git", "add", "README.md" ])

print "pre-commit hook complete."
Dan Dye
  • 767
  • 7
  • 15
  • Getting the repo and github user is tricky and somewhat fragile since there is no guaranteed information of where the repository comes from. You can use the repo-URL if you're prepared to live with the fragility: ```REPOurl=subprocess.check_output(['git','config','--local', 'remote.origin.url']).decode() ``` – DrSAR Jun 11 '16 at 05:25
  • ```GITHUB_USER=re.match('.*:([a-zA-Z0-9]*)\/', REPOurl).groups()[0] ``` – DrSAR Jun 11 '16 at 05:29
  • ```REPO=re.match('.*\/([a-zA-Z0-9]*).git', REPOurl).groups()[0] ``` – DrSAR Jun 11 '16 at 05:29
  • 3
    That's cool, I had thought about this but I wish I could have a magic variable that refers to the current branch in github so that my commit history doesn't get polluted. – Andy Feb 19 '17 at 19:02
5

I updated the work of Dan Dye so it's now able to change any git variable into a readme. It also works now with python 3. For example, handling badges by branch for Github actions:

[![Integration Tests](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml/badge.svg?branch={{ current.branch }})](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml?query=branch%3A{{ current.branch }})

And in your pre-commit file add:

.githooks/replace_by_git_vars.py readme.md README.md -v

-v displays the available variables and more

https://gist.github.com/jclaveau/af2271b9fdf05f7f1983f492af5592f8

Thanks a lot for the solution and inspiration!

Jean Claveau
  • 1,101
  • 1
  • 13
  • 15
0

The best solution for me was to create a server where I send a query with username and repo's name and get a svg image with the build status for all branches.