背景:项目中需要展示GITHUB_REF_NAME。在文档中的定义为:如果存在当前commit的tag,则展示tag名,否则,展示分支名
https://docs.github.com/en/actions/learn-github-actions/environment-variables
首先:你需要了解tag的分类:
tag主要有两类:
- 轻量标签(lightweight)
- 附注标签(annotated)
两种标签的创建命令是:
# 创建一个名为 v0.0.1 的轻量 tag
git tag v0.0.1
# 创建一个名为 v1.0.0 的附注 tag
git tag -a v1.0.0 -m 'tag description'
# 将 tag 推送至远程
git push origin v1.0.0 # ggp v1.0.0
显示标签:注意显示标签的顺序与创建的先后无关
git tag
如何判断tag的类型
git show tagname显示两种标签的区别(注意红框内容)
附注标签(annotated):包含tagger
轻量标签(lightweight):不包含tagger
git describe
git describe 该命令查找提交中可到达的最近的标记
结果:
v1.8.3-rc2-10-g0c2b1cf
10:表示自打tag v1.8.3-rc2 以来有10次提交(commit)
g0c2b1cf:g 为git的缩写,在多种管理工具并存的环境中很有用处;
0c2b1cf:7位字符表示为最新提交的commit id 前7位
参数介绍:
-tags:代表包含所有的tag(默认只包括附注标签)
GITHUB_REF_NAME
git describe --tags --exact-match --abbrev=0
得到本commit的tag
git rev-parse --abbrev-ref HEAD
得到当前分支名
let tagname=''
let brachname=require('child_process').execSync('git rev-parse --abbrev-ref HEAD')
try{
tagname = require('child_process').execSync('git describe --tags --exact-match --abbrev=0')
}catch(e){
tagname=brachname;
}
process.env.VUE_APP_VERSION = `${tagname}-${commit} (${date})`
//显示当前commit的只包含附注标签:
git describe --exact-match --abbrev=0
//显示当前commit的tag(包含轻量标签):
git describe --tags --exact-match --abbrev=0
//最近一个tag
git describe --tags $(git rev-list --tags --max-count=1)
//当前的分支名
git rev-parse --abbrev-ref HEAD
参考链接:
【1】:https://gb.yekai.net/concepts/git-tag
【2】:https://git-scm.com/docs/git-describe
【3】:https://git-scm.com/docs/git-tag
【4】:https://blog.csdn.net/wh_19910525/article/details/78599647