Joe*_*att 82 markdown github travis-ci
在我的github repo的readme.md文件中,我有一个Travis-CI徽章.我使用以下链接:
https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging
Run Code Online (Sandbox Code Playgroud)
显而易见的问题是分支是硬编码的.是否可以使用某种变量,以便分支是当前正在查看的分支?
Von*_*onC 51
从来没听说过.
GitHub支持确认(通过OP Joe Gatt的评论)
执行此操作的唯一方法是通过我自己的服务传递链接,该服务将使用github的http referrer标头来确定正在引用哪个分支,然后从Travis CI获取相应的图像
我宁愿每个分支制作一个Travis-CI徽章,供读者在看到时选择或考虑合适README.md
.
2016年更新(3年后):虽然GitHub方面没有任何变化,但fedorqui在Andrie的 " 获取Traith Shield on Github以反映选定的分支状态 "中提到的变通方法中报告.
只需显示所有分支及其各自的TravisCI徽章.
如果你只有两三个分支,那就足够了.
Dan*_*Dye 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:
[][travis]
with:
[][travis]
pre-commit hook complete.
[feature123 54897ee] Added Feature123
1 file changed, 2 insertions(+), 1 deletion(-)
dandye$ cat README.md |grep "Build Status"
[][travis]
dandye$
Run Code Online (Sandbox Code Playgroud)
dandye$ cat .git/hooks/pre-commit
Run Code Online (Sandbox Code Playgroud)
#!/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)
# [][travis]
# Output String with Variable substitution
travis="[][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."
Run Code Online (Sandbox Code Playgroud)
我更新了它的工作Dan Dye
,因此现在可以将任何 git 变量更改为自述文件。它现在也适用于 python 3。例如,按分支处理 Github 操作的徽章:
[](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml?query=branch%3A{{ current.branch }})
Run Code Online (Sandbox Code Playgroud)
并在您的pre-commit
文件中添加:
.githooks/replace_by_git_vars.py readme.md README.md -v
Run Code Online (Sandbox Code Playgroud)
-v
显示可用变量等
https://gist.github.com/jclaveau/af2271b9fdf05f7f1983f492af5592f8
非常感谢您的解决方案和灵感!