feat(trigger-version): 添加标签过滤功能支持通配符匹配和排除

新增 `tag-match` 和 `tag-exclude` 输入参数,支持通过通配符模式匹配和排除特定标签
更新 README 文档并添加示例配置文件展示标签过滤功能的使用场景
This commit is contained in:
Lyda
2025-08-22 13:22:31 +08:00
parent b53d899f41
commit 6fff7a90e1
3 changed files with 310 additions and 21 deletions

View File

@@ -11,6 +11,14 @@ inputs:
description: "在非版本触发时是否使用当前分支最新的版本标签默认false"
required: false
default: "false"
tag-match:
description: "标签匹配模式支持通配符v*.*.* 或 v[0-9]*),用于进一步过滤标签"
required: false
default: ""
tag-exclude:
description: "标签排除模式,支持通配符(如:*-alpha* 或 *-beta*),匹配的标签将被排除"
required: false
default: ""
outputs:
ref-type:
@@ -51,10 +59,14 @@ runs:
# 获取最新版本号的函数
get_latest_version() {
local prefix="$1"
local tag_match="$2"
local tag_exclude="$3"
local latest_version=""
local latest_version_with_dash=""
echo "🔍 开始获取最新版本号(前缀:${prefix}..."
[[ -n "$tag_match" ]] && echo "🎯 标签匹配模式: $tag_match"
[[ -n "$tag_exclude" ]] && echo "🚫 标签排除模式: $tag_exclude"
# 检查git仓库是否可用
if git rev-parse --git-dir > /dev/null 2>&1; then
@@ -69,25 +81,55 @@ runs:
local all_tags=$(git tag --list "${prefix}*" 2>/dev/null)
if [[ -n "$all_tags" ]]; then
# echo "找到的标签:"
# echo "$all_tags"
# 获取最新的版本标签
if command -v sort >/dev/null 2>&1; then
# 使用 sort 命令进行版本排序
latest_version=$(echo "$all_tags" | sort -V | tail -1)
else
# 如果没有 sort -V使用 git 的排序
latest_version=$(git tag --list "${prefix}*" --sort=-version:refname 2>/dev/null | head -1)
# 应用标签匹配过滤
if [[ -n "$tag_match" ]]; then
echo "🔍 应用标签匹配过滤: $tag_match"
local filtered_tags=""
while IFS= read -r tag; do
if [[ "$tag" == $tag_match ]]; then
filtered_tags="$filtered_tags$tag\n"
fi
done <<< "$all_tags"
all_tags=$(echo -e "$filtered_tags" | sed '/^$/d')
echo "📋 匹配后的标签数量: $(echo "$all_tags" | wc -l | tr -d ' ')"
fi
if [[ -n "$latest_version" ]]; then
# 生成带横线的版本号
latest_version_with_dash=$(echo "$latest_version" | sed 's/\./-/g')
echo "📦 找到最新版本: $latest_version"
echo "📦 横线格式版本: $latest_version_with_dash"
# 应用标签排除过滤
if [[ -n "$tag_exclude" && -n "$all_tags" ]]; then
echo "🚫 应用标签排除过滤: $tag_exclude"
local filtered_tags=""
while IFS= read -r tag; do
if [[ "$tag" != $tag_exclude ]]; then
filtered_tags="$filtered_tags$tag\n"
fi
done <<< "$all_tags"
all_tags=$(echo -e "$filtered_tags" | sed '/^$/d')
echo "📋 排除后的标签数量: $(echo "$all_tags" | wc -l | tr -d ' ')"
fi
if [[ -n "$all_tags" ]]; then
# echo "最终标签列表:"
# echo "$all_tags"
# 获取最新的版本标签
if command -v sort >/dev/null 2>&1; then
# 使用 sort 命令进行版本排序
latest_version=$(echo "$all_tags" | sort -V | tail -1)
else
# 如果没有 sort -V使用简单的字典序排序
latest_version=$(echo "$all_tags" | sort | tail -1)
fi
if [[ -n "$latest_version" ]]; then
# 生成带横线的版本号
latest_version_with_dash=$(echo "$latest_version" | sed 's/\./-/g')
echo "📦 找到最新版本: $latest_version"
echo "📦 横线格式版本: $latest_version_with_dash"
else
echo "⚠️ 未能确定最新版本"
fi
else
echo "⚠️ 未能确定最新版本"
echo "⚠️ 过滤后没有找到匹配的版本标签"
fi
else
echo "⚠️ 未找到匹配前缀 '${prefix}' 的版本标签"
@@ -112,9 +154,11 @@ runs:
FULL_REF="${{ github.ref }}"
VERSION_PREFIX="${{ inputs.version-prefix }}"
USE_LATEST_VERSION="${{ inputs.use-latest-version }}"
TAG_MATCH="${{ inputs.tag-match }}"
TAG_EXCLUDE="${{ inputs.tag-exclude }}"
# 获取最新版本号(无论触发方式如何都获取)
get_latest_version "$VERSION_PREFIX"
get_latest_version "$VERSION_PREFIX" "$TAG_MATCH" "$TAG_EXCLUDE"
# 判断是否为标签触发
if [[ "$REF_TYPE" == "tag" ]]; then