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

@@ -9,16 +9,19 @@
- 🆕 **常规分支处理**:对于非版本分支提供基础信息
- 📦 **最新版本获取**:始终获取仓库中的最新版本号,无论触发方式如何
- 🎯 **灵活的版本前缀**:支持自定义版本前缀(默认为 `v`
- 🔍 **标签过滤功能**:支持通配符模式匹配和排除特定标签
- 🔧 **版本格式转换**:自动生成横线格式的版本号(如 `v1.2.3``v1-2-3`
- 📤 **环境变量输出**:自动设置环境变量供后续步骤使用
- 📊 **详细的输出信息**:提供完整的引用信息和触发状态
## 输入参数
| 参数名 | 描述 | 必需 | 默认值 |
| -------------------- | -------------------------------- | ---- | ------- |
| `version-prefix` | 版本前缀,用于匹配版本标签或分支 | 否 | `v` |
| `use-latest-version` | 在非版本触发时是否使用最新版本 | 否 | `false` |
| 参数名 | 描述 | 必需 | 默认值 |
| -------------------- | ------------------------------------------------------------ | ---- | ------- |
| `version-prefix` | 版本前缀,用于匹配版本标签或分支 | 否 | `v` |
| `use-latest-version` | 在非版本触发时是否使用最新版本 | 否 | `false` |
| `tag-match` | 标签匹配模式,支持通配符(如:`v*.*.*``v[0-9]*`),用于进一步过滤标签 | 否 | `""` |
| `tag-exclude` | 标签排除模式,支持通配符(如:`*-alpha*``*-beta*`),匹配的标签将被排除 | 否 | `""` |
## 输出参数
@@ -119,6 +122,73 @@ jobs:
uses: actions/xgj/trigger-version@v1
with:
version-prefix: "release-"
```
### 标签过滤功能
#### 排除预发布版本
```yaml
- name: 获取稳定版本(排除预发布)
id: stable-version
uses: ./trigger-version
with:
version-prefix: "v"
use-latest-version: true
tag-exclude: "*-alpha*" # 排除包含 alpha 的版本
- name: 显示稳定版本
run: |
echo "最新稳定版本: ${{ steps.stable-version.outputs.latest-version }}"
```
#### 匹配特定版本模式
```yaml
- name: 获取标准语义版本
id: semver
uses: ./trigger-version
with:
version-prefix: "v"
use-latest-version: true
tag-match: "v[0-9]*.[0-9]*.[0-9]*" # 只匹配 v1.2.3 格式
- name: 显示语义版本
run: |
echo "语义版本: ${{ steps.semver.outputs.latest-version }}"
```
#### 复合过滤条件
```yaml
- name: 获取 v2.x 稳定版本
id: v2-stable
uses: ./trigger-version
with:
version-prefix: "v"
use-latest-version: true
tag-match: "v2.*.*" # 只匹配 v2.x.x 版本
tag-exclude: "*-*" # 排除所有预发布版本(包含连字符)
- name: 显示 v2.x 稳定版本
run: |
echo "v2.x 最新稳定版本: ${{ steps.v2-stable.outputs.latest-version }}"
```
#### 获取预发布版本
```yaml
- name: 获取最新 beta 版本
id: beta-version
uses: ./trigger-version
with:
version-prefix: "v"
use-latest-version: true
tag-match: "*-beta*" # 只匹配包含 beta 的版本
- name: 显示 beta 版本
run: |
echo "最新 beta 版本: ${{ steps.beta-version.outputs.latest-version }}"
````
### 完整的 CI/CD 流程
@@ -180,6 +250,44 @@ jobs:
echo "部署版本 ${{ needs.analyze.outputs.trigger-version }} 到生产环境"
```
## 标签过滤功能详解
### 通配符模式支持
`tag-match` 和 `tag-exclude` 参数支持 bash 通配符模式:
- `*`:匹配任意字符序列
- `?`:匹配单个字符
- `[...]`:匹配括号内的任意字符
- `[!...]`:匹配不在括号内的任意字符
### 常用过滤模式
| 模式 | 描述 | 示例匹配 |
| ------------------- | ------------------------------ | --------------------------- |
| `v*.*.*` | 匹配标准三段式版本号 | `v1.2.3`, `v2.0.1` |
| `v[0-9]*.[0-9]*.*` | 匹配数字开头的版本号 | `v1.2.3`, `v10.0.1` |
| `*-alpha*` | 匹配包含 alpha 的版本 | `v1.0.0-alpha1` |
| `*-beta*` | 匹配包含 beta 的版本 | `v1.0.0-beta2` |
| `*-rc*` | 匹配包含 rc 的版本 | `v1.0.0-rc1` |
| `*-*` | 匹配所有包含连字符的版本 | `v1.0.0-alpha`, `v1.0.0-1` |
| `v2.*` | 匹配 v2 开头的所有版本 | `v2.0.0`, `v2.1.5` |
| `v[12].*.*` | 匹配 v1 或 v2 开头的版本 | `v1.0.0`, `v2.3.1` |
### 过滤优先级
1. 首先应用 `version-prefix` 过滤(获取匹配前缀的标签)
2. 然后应用 `tag-match` 过滤(保留匹配模式的标签)
3. 最后应用 `tag-exclude` 过滤(排除匹配模式的标签)
4. 对剩余标签进行版本排序,选择最新版本
### 注意事项
- 标签过滤主要影响 `latest-version` 的获取,不影响当前触发版本的识别
- 当通过标签触发时,`trigger-version` 始终是触发的标签,过滤只影响 `latest-version`
- 如果过滤后没有匹配的标签,`latest-version` 将为空
- 建议在使用过滤功能时设置 `fetch-depth: 0` 以获取完整的标签历史
## 触发场景
### 标签触发

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

View File

@@ -0,0 +1,137 @@
name: Tag Filtering Demo
# 演示如何使用 tagMatch 和 tagExclude 功能来过滤版本标签
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
# 示例1只匹配稳定版本排除预发布版本
stable-versions-only:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整的 git 历史
- name: Get stable version (exclude alpha/beta/rc)
id: stable-version
uses: ./trigger-version
with:
version-prefix: 'v'
use-latest-version: 'true'
tag-exclude: '*-alpha*|*-beta*|*-rc*'
- name: Display stable version info
run: |
echo "Latest stable version: ${{ steps.stable-version.outputs.latest-version }}"
echo "Version with dash: ${{ steps.stable-version.outputs.latest-version-with-dash }}"
# 示例2只匹配特定模式的版本
specific-pattern-match:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get version matching specific pattern
id: pattern-version
uses: ./trigger-version
with:
version-prefix: 'v'
use-latest-version: 'true'
tag-match: 'v[0-9]*.[0-9]*.[0-9]*' # 只匹配标准的语义版本格式
- name: Display pattern matched version
run: |
echo "Pattern matched version: ${{ steps.pattern-version.outputs.latest-version }}"
# 示例3复杂过滤 - 匹配主版本号并排除预发布
complex-filtering:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get v2.x versions excluding pre-releases
id: v2-stable
uses: ./trigger-version
with:
version-prefix: 'v'
use-latest-version: 'true'
tag-match: 'v2.*.*' # 只匹配 v2.x.x 版本
tag-exclude: '*-*' # 排除所有包含连字符的版本(预发布版本)
- name: Display filtered version
run: |
echo "Latest v2.x stable version: ${{ steps.v2-stable.outputs.latest-version }}"
# 示例4获取预发布版本
prerelease-versions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get latest beta version
id: beta-version
uses: ./trigger-version
with:
version-prefix: 'v'
use-latest-version: 'true'
tag-match: '*-beta*' # 只匹配包含 beta 的版本
- name: Display beta version
run: |
echo "Latest beta version: ${{ steps.beta-version.outputs.latest-version }}"
# 示例5多条件排除
multi-exclude:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get version excluding multiple patterns
id: multi-exclude
uses: ./trigger-version
with:
version-prefix: 'v'
use-latest-version: 'true'
# 注意:多个排除模式需要在 shell 脚本中处理,这里展示单个模式
tag-exclude: '*-alpha*' # 排除 alpha 版本
- name: Display filtered version
run: |
echo "Version (excluding alpha): ${{ steps.multi-exclude.outputs.latest-version }}"
# 示例6在版本触发时的行为
version-trigger-behavior:
runs-on: ubuntu-latest
if: github.ref_type == 'tag'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Process version trigger
id: version-trigger
uses: ./trigger-version
with:
version-prefix: 'v'
# 注意当通过标签触发时tag-match 和 tag-exclude 主要影响 latest-version 的获取
# 而不影响当前触发的版本
tag-exclude: '*-alpha*'
- name: Display trigger info
run: |
echo "Triggered by tag: ${{ steps.version-trigger.outputs.trigger-version }}"
echo "Latest stable version: ${{ steps.version-trigger.outputs.latest-version }}"
echo "Is version trigger: ${{ steps.version-trigger.outputs.is-version-trigger }}"