From 7837d0cbea154456025f34dd26115564668592aa Mon Sep 17 00:00:00 2001 From: godcong Date: Thu, 11 Jul 2024 02:20:59 +0800 Subject: [PATCH] feat(github-actions): add script to get latest tag for current commit Add a Bash script to retrieve the latest Git tag associated with the current commit. The script configures Git user information, extracts the current commit hash, finds any tags pointing at the commit, and displays the most recent tag if available. Assumptions: - Tags are annotated and follow a time-based ordering. --- scripts/git/commit_tag.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/git/commit_tag.sh diff --git a/scripts/git/commit_tag.sh b/scripts/git/commit_tag.sh new file mode 100644 index 0000000..9f84dd5 --- /dev/null +++ b/scripts/git/commit_tag.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# 设置 Git 用户信息 +git config user.name "GitHub Action" +git config user.email "action@github.com" + +#git config user.name "godcong" +#git config user.email "jumbycc@163.com" + +# 获取当前HEAD的哈希值 +CURRENT_COMMIT=$(git rev-parse HEAD) + +# 查找与当前提交匹配的标签 +TAG_FOR_CURRENT_COMMIT=$(git tag --points-at "$CURRENT_COMMIT") + +# 检查是否有标签 +if [ -n "$TAG_FOR_CURRENT_COMMIT" ]; then + # 将标签列表转换为数组 + IFS=$'\n' read -d '' -r -a TAGS_ARRAY <<< "$TAG_FOR_CURRENT_COMMIT" + # 找到最新的标签,假设标签按时间顺序排列 + LATEST_TAG=${TAGS_ARRAY[-1]} + echo "$LATEST_TAG" +fi \ No newline at end of file