在我的github reporeadme.md
文件中,我有一个Travis-CI徽章。我使用以下链接:
https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging
明显的问题是分支是硬编码的。是否可以使用某种变量,以便使分支成为当前正在查看的分支?
Answers:
从来没听说过。
GitHub支持确认(通过OP Joe Gatt的评论))
唯一的方法是通过我自己的服务传递链接,该服务将使用github的http Referrer标头确定正在引用的分支,然后从Travis CI中获取适当的图像
我宁愿为每个分支机构制作一个Travis-CI徽章,以便读者在看到 README.md
。
2016年更新(3年后):尽管GitHub方面没有任何变化,但fedorqui报告了Andrie的“在Github上获取Travis Shield以反映所选的分支状态”中提到的解决方法。
只需显示所有分支及其各自的TravisCI徽章。
如果您只有两个或三个分支,那就足够了。
HTTP_REFERER
当从GitHub上的README加载图像时,我没有得到。:-(
我使用一个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$
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."
REPOurl=subprocess.check_output(['git','config','--local', 'remote.origin.url']).decode()
GITHUB_USER=re.match('.*:([a-zA-Z0-9]*)\/', REPOurl).groups()[0]
REPO=re.match('.*\/([a-zA-Z0-9]*).git', REPOurl).groups()[0]
对我而言,最好的解决方案是创建一个服务器,在该服务器上发送带有用户名和仓库名称的查询,并获取具有所有分支的构建状态的svg映像。