在我的 github reporeadme.md
文件中,我有一个 Travis-CI 徽章。我使用以下链接:
https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging
明显的问题是分支是硬编码的。是否可以使用某种变量以使分支是当前正在查看的分支?
从来没听说过。
GitHub 支持确认(通过OP Joe Gatt的评论)
这样做的唯一方法是通过我自己的服务传递链接,该服务将使用 github 的 http referrer 标头来确定正在引用哪个分支,然后从 Travis CI 获取适当的图像
我宁愿为每个分支制作一个 Travis-CI 徽章,供读者在看到README.md
.
2016 年更新(3 年后):虽然 GitHub 方面没有任何变化,但Fedorqui报告了Andrie在“在 Github 上获取 Travis Shield 以反映所选分支状态”中提到的解决方法。
只需显示所有分支及其各自的 TravisCI 徽章。
如果你只有两三个分支,那就足够了。
我使用 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."
我更新了工作,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
非常感谢您的解决方案和灵感!
对我来说最好的解决方案是创建一个服务器,我在其中发送一个带有用户名和 repo 名称的查询,并获取一个包含所有分支构建状态的 svg 图像。