在github readme.md中引用当前分支

时间:2013-09-07 13:17:52

标签: github markdown travis-ci

在我的github repo的readme.md文件中,我有一个Travis-CI徽章。我使用以下链接:

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

显而易见的问题是分支是硬编码的。是否可以使用某种变量,以便分支是当前正在查看的分支?

4 个答案:

答案 0 :(得分:51)

不是我知道的。
GitHub support确认(通过OP Joe Gattcomment

  

执行此操作的唯一方法是通过我自己的服务传递链接,该服务将使用github的http referrer标头来确定正在引用哪个分支,然后从Travis CI中获取相应的图像

我宁愿为每个分支制作一个Travis-CI徽章,供读者在看到README.md时选择或考虑合适。


2016年更新(3年后):虽然GitHub方面没有任何变化,fedorqui报告了Get Travis Shield on Github to Reflect Selected Branch Status中“Andrie”中提及的解决方法。 只需显示所有分支及其各自的TravisCI徽章。

如果你只有两个或三个分支,那就足够了。

答案 1 :(得分:13)

我使用git pre-commit钩子解决了这个问题,该钩子使用当前分支在README.md中重写Travis行。下面是使用和预提交(Python)代码(针对问题的问题)的一个示例。

用法

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."

答案 2 :(得分:1)

我更新了 Dan Dye 的工作,因此它现在可以将任何 git 变量更改为自述文件。它现在也适用于 python 3。例如,通过分支处理 Github 操作的徽章:

[![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 }})

并在您的 pre-commit 文件中添加:

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

-v 显示可用变量等

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

非常感谢您的解决方案和灵感!

答案 3 :(得分:0)

对我来说,最好的解决方案是创建一个服务器,在该服务器上发送带有用户名和仓库名称的查询,并获取具有所有分支的构建状态的svg映像。