From e4063690cdf15d6db103c350423514c8520aaf72 Mon Sep 17 00:00:00 2001 From: Lyda <1829913225@qq.com> Date: Wed, 20 Aug 2025 19:29:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20Web=20=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=8F=91=E5=B8=83=E6=9E=84=E5=BB=BA=E7=9A=84=20GitHub?= =?UTF-8?q?=20Action=EF=BC=8C=E7=A1=AE=E4=BF=9D=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E7=BB=9F=E4=B8=80=E6=B7=BB=E5=8A=A0=20v=20?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=8C=E4=BC=98=E5=8C=96=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E4=BB=A5=E5=8F=8D=E6=98=A0=E6=96=B0=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=92=8C=E4=BD=BF=E7=94=A8=E7=A4=BA=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- release-web/.release-it.js | 2 +- release-web/README.md | 19 ++++----- release-web/action.yml | 33 +++++++++------ .../examples/custom-version-source.yml | 4 +- release-web/examples/version-handling.yml | 40 +++++++++---------- 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/release-web/.release-it.js b/release-web/.release-it.js index b8dbb4e..3384dc0 100644 --- a/release-web/.release-it.js +++ b/release-web/.release-it.js @@ -121,7 +121,7 @@ module.exports = { }, hooks: { 'before:release': [ - `echo '\${version}' > /tmp/last-version`, + `echo 'v\${version}' > /tmp/last-version`, 'git add --all', 'npm run build' ], diff --git a/release-web/README.md b/release-web/README.md index fb333f4..9ca6ba8 100644 --- a/release-web/README.md +++ b/release-web/README.md @@ -80,7 +80,8 @@ Action 自动处理各种版本格式,确保输出一致: # - package.json: "v1.2.3" 或 "1.2.3" # - 提交信息: "release: v1.2.3" 或 "release: 1.2.3" -# 输出始终为: "1.2.3" (不含 v 前缀) +# 输出始终为: "v1.2.3" (统一添加 v 前缀) +# 带横线版本: "v1-2-3" (保持 v 前缀) ``` ## 输入参数 @@ -125,8 +126,8 @@ Action 支持通过 `env` 传递**任意数量**的环境变量给发布命令 | 参数名 | 描述 | | ------------------- | -------------------------------------- | -| `version` | 发布的版本号 | -| `version-with-dash` | 版本号(点号替换为横线) | +| `version` | 发布的版本号(带 v 前缀) | +| `version-with-dash` | 版本号(带 v 前缀,点号替换为横线) | | `release-status` | 发布命令执行状态 (`success`/`failure`) | | `exit-code` | 发布命令退出码(仅在失败时有值) | @@ -303,10 +304,10 @@ steps: 2. **智能版本获取**: 只在发布成功时执行,通过多种方法自动获取版本号 - 优先从版本文件读取(`/tmp/last-version` 或自定义路径) - - 回退到最新 git tag(自动去除 `v` 前缀) + - 回退到最新 git tag(保持原始格式) - 回退到 `package.json` 中的版本 - 最后尝试从提交信息解析(支持 `v1.2.3` 和 `1.2.3` 格式) - - **所有方法都确保输出不包含 `v` 前缀** + - **所有方法都确保输出统一包含 `v` 前缀** 3. **发布总结**: 显示详细的发布状态信息 @@ -342,7 +343,7 @@ steps: module.exports = { hooks: { "before:release": [ - "echo '${version}' > /tmp/last-version", + "echo 'v${version}' > /tmp/last-version", "npm run build", ], }, @@ -465,7 +466,7 @@ A: Action 会按优先级尝试多种方法获取版本: ```javascript // .release-it.js hooks: { - 'before:release': ['echo \'${version}\' > /tmp/last-version'] + 'before:release': ['echo \'v${version}\' > /tmp/last-version'] } ``` @@ -486,9 +487,9 @@ A: Action 会按优先级尝试多种方法获取版本: 4. **提交信息**:在提交信息中包含版本号 ```bash - git commit -m "release: 1.2.3" - # 或者 git commit -m "release: v1.2.3" + # 或者 + git commit -m "release: 1.2.3" ``` ### Q: Gitea Token 权限不足 diff --git a/release-web/action.yml b/release-web/action.yml index e1779cf..7621fa3 100644 --- a/release-web/action.yml +++ b/release-web/action.yml @@ -29,11 +29,11 @@ inputs: outputs: version: - description: '发布的版本号' + description: '发布的版本号(带 v 前缀)' value: ${{ steps.get_var.outputs.version }} version-with-dash: - description: '版本号(点号替换为横线)' + description: '版本号(带 v 前缀,点号替换为横线)' value: ${{ steps.get_var.outputs.version_with_dash }} release-status: @@ -82,15 +82,15 @@ runs: # 方法1: 从指定版本文件读取(优先级最高) if [[ -f "${{ inputs.version-file }}" ]]; then - VERSION=$(cat "${{ inputs.version-file }}" | tr -d ' \n\r' | sed 's/^v//') + VERSION=$(cat "${{ inputs.version-file }}" | tr -d ' \n\r') echo "✅ 从版本文件获取: $VERSION" # 方法2: 从最新的 git tag 获取 elif git describe --tags --abbrev=0 2>/dev/null; then - VERSION=$(git describe --tags --abbrev=0 | tr -d ' \n\r' | sed 's/^v//') + VERSION=$(git describe --tags --abbrev=0 | tr -d ' \n\r') echo "✅ 从 git tag 获取: $VERSION" # 方法3: 从 package.json 获取 elif [[ -f "package.json" ]] && command -v node >/dev/null; then - VERSION=$(node -p "require('./package.json').version" 2>/dev/null | tr -d ' \n\r' | sed 's/^v//') + VERSION=$(node -p "require('./package.json').version" 2>/dev/null | tr -d ' \n\r') if [[ -n "$VERSION" && "$VERSION" != "undefined" ]]; then echo "✅ 从 package.json 获取: $VERSION" else @@ -102,7 +102,7 @@ runs: if [[ -z "$VERSION" ]]; then echo "⚠️ 无法通过常规方法获取版本,尝试解析最近的提交信息..." # 尝试从最近的提交消息中解析版本(如 "release: 1.2.3" 或 "release: v1.2.3" 格式) - COMMIT_VERSION=$(git log -1 --pretty=format:"%s" | grep -oE 'v?[0-9]+\.[0-9]+\.[0-9]+' | head -1 | sed 's/^v//') + COMMIT_VERSION=$(git log -1 --pretty=format:"%s" | grep -oE 'v?[0-9]+\.[0-9]+\.[0-9]+' | head -1) if [[ -n "$COMMIT_VERSION" ]]; then VERSION="$COMMIT_VERSION" echo "✅ 从提交信息获取: $VERSION" @@ -113,19 +113,26 @@ runs: if [[ -z "$VERSION" ]]; then echo "❌ 错误: 无法获取版本信息" echo "请确保:" - echo " 1. 发布命令生成 /tmp/last-version 文件,或" - echo " 2. 存在有效的 git tag,或" + echo " 1. 发布命令生成版本文件(推荐格式: v1.2.3),或" + echo " 2. 存在有效的 git tag(格式: v1.2.3),或" echo " 3. package.json 包含 version 字段,或" - echo " 4. 提交信息包含版本号" + echo " 4. 提交信息包含版本号(格式: release: v1.2.3)" exit 1 fi - # 确保版本号格式正确(去掉可能的 v 前缀和空白字符) - VERSION=$(echo "$VERSION" | sed 's/^v//' | tr -d ' \n\r') + # 确保版本号格式正确(清理空白字符并确保 v 前缀) + VERSION=$(echo "$VERSION" | tr -d ' \n\r') + + # 确保版本号有 v 前缀 + if [[ ! "$VERSION" =~ ^v ]]; then + VERSION="v$VERSION" + fi + + # 创建带横线的版本号(保持 v 前缀) VERSION_WITH_DASH=$(echo "$VERSION" | sed 's/\./-/g') - # 验证版本号格式 - if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-].*)?$ ]]; then + # 验证版本号格式(应该以 v 开头) + if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-].*)?$ ]]; then echo "⚠️ 警告: 版本号格式可能不标准: $VERSION" fi diff --git a/release-web/examples/custom-version-source.yml b/release-web/examples/custom-version-source.yml index d1d84c7..0d7dff0 100644 --- a/release-web/examples/custom-version-source.yml +++ b/release-web/examples/custom-version-source.yml @@ -28,7 +28,7 @@ jobs: - name: 发布(自定义版本文件) uses: actions/xgj/release-web@main with: - release-command: "echo '1.2.3' > ./custom-version.txt" + release-command: "echo 'v1.2.3' > ./custom-version.txt" version-file: "./custom-version.txt" env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} @@ -63,7 +63,7 @@ jobs: run: | git config user.name "github-actions" git config user.email "github-actions@github.com" - git commit --allow-empty -m "release: 1.2.6" + git commit --allow-empty -m "release: v1.2.6" - name: 发布(基于提交信息) uses: actions/xgj/release-web@main diff --git a/release-web/examples/version-handling.yml b/release-web/examples/version-handling.yml index ca59d34..cdbe4d9 100644 --- a/release-web/examples/version-handling.yml +++ b/release-web/examples/version-handling.yml @@ -37,13 +37,13 @@ jobs: env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} - - name: 验证版本输出(不应包含 v) + - name: 验证版本输出(应包含 v 前缀) run: | echo "版本输出: ${{ steps.test1.outputs.version }}" - if [[ "${{ steps.test1.outputs.version }}" == "1.2.3" ]]; then - echo "✅ 正确:版本文件 v 前缀已去除" + if [[ "${{ steps.test1.outputs.version }}" == "v1.2.3" ]]; then + echo "✅ 正确:版本文件 v 前缀保持不变" else - echo "❌ 错误:版本应该是 1.2.3,实际是 ${{ steps.test1.outputs.version }}" + echo "❌ 错误:版本应该是 v1.2.3,实际是 ${{ steps.test1.outputs.version }}" exit 1 fi @@ -67,10 +67,10 @@ jobs: - name: 验证 git tag 版本输出 run: | echo "Git tag 版本输出: ${{ steps.test2.outputs.version }}" - if [[ "${{ steps.test2.outputs.version }}" == "1.2.4" ]]; then - echo "✅ 正确:Git tag v 前缀已去除" + if [[ "${{ steps.test2.outputs.version }}" == "v1.2.4" ]]; then + echo "✅ 正确:Git tag v 前缀保持不变" else - echo "❌ 错误:版本应该是 1.2.4,实际是 ${{ steps.test2.outputs.version }}" + echo "❌ 错误:版本应该是 v1.2.4,实际是 ${{ steps.test2.outputs.version }}" exit 1 fi @@ -93,10 +93,10 @@ jobs: - name: 验证 package.json 版本输出 run: | echo "Package.json 版本输出: ${{ steps.test3.outputs.version }}" - if [[ "${{ steps.test3.outputs.version }}" == "1.2.5" ]]; then - echo "✅ 正确:Package.json v 前缀已去除" + if [[ "${{ steps.test3.outputs.version }}" == "v1.2.5" ]]; then + echo "✅ 正确:Package.json 版本已添加 v 前缀" else - echo "❌ 错误:版本应该是 1.2.5,实际是 ${{ steps.test3.outputs.version }}" + echo "❌ 错误:版本应该是 v1.2.5,实际是 ${{ steps.test3.outputs.version }}" exit 1 fi @@ -120,10 +120,10 @@ jobs: - name: 验证提交信息版本输出 run: | echo "提交信息版本输出: ${{ steps.test4.outputs.version }}" - if [[ "${{ steps.test4.outputs.version }}" == "1.2.6" ]]; then - echo "✅ 正确:提交信息 v 前缀已去除" + if [[ "${{ steps.test4.outputs.version }}" == "v1.2.6" ]]; then + echo "✅ 正确:提交信息 v 前缀保持不变" else - echo "❌ 错误:版本应该是 1.2.6,实际是 ${{ steps.test4.outputs.version }}" + echo "❌ 错误:版本应该是 v1.2.6,实际是 ${{ steps.test4.outputs.version }}" exit 1 fi @@ -131,18 +131,18 @@ jobs: - name: 验证版本带横线格式 run: | echo "版本带横线: ${{ steps.test4.outputs.version-with-dash }}" - if [[ "${{ steps.test4.outputs.version-with-dash }}" == "1-2-6" ]]; then + if [[ "${{ steps.test4.outputs.version-with-dash }}" == "v1-2-6" ]]; then echo "✅ 正确:版本横线格式正确" else - echo "❌ 错误:版本横线格式应该是 1-2-6,实际是 ${{ steps.test4.outputs.version-with-dash }}" + echo "❌ 错误:版本横线格式应该是 v1-2-6,实际是 ${{ steps.test4.outputs.version-with-dash }}" exit 1 fi - name: 测试总结 run: | echo "🎉 所有版本处理测试通过!" - echo "✅ 版本文件 v 前缀处理正确" - echo "✅ Git tag v 前缀处理正确" - echo "✅ Package.json v 前缀处理正确" - echo "✅ 提交信息 v 前缀处理正确" - echo "✅ 版本横线格式正确" + echo "✅ 版本文件 v 前缀保持不变" + echo "✅ Git tag v 前缀保持不变" + echo "✅ Package.json 版本自动添加 v 前缀" + echo "✅ 提交信息 v 前缀保持不变" + echo "✅ 版本横线格式包含 v 前缀"