feat: 增强 Web 项目发布构建的 GitHub Action,添加自定义版本文件路径支持,优化版本获取逻辑,确保版本号格式一致,更新文档以反映新功能和使用示例。

This commit is contained in:
Lyda
2025-08-20 17:53:45 +08:00
parent 59104cf204
commit c039fac36c
5 changed files with 342 additions and 18 deletions

View File

@@ -0,0 +1,148 @@
name: 版本处理示例
on:
push:
branches: [main]
jobs:
test-version-handling:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 设置环境
uses: actions/xgj/setup-env@main
with:
docker-registry: "docker-registry.bjxgj.com"
docker-username: ${{ secrets.DOCKER_USERNAME }}
docker-password: ${{ secrets.DOCKER_PASSWORD }}
- name: 安装依赖
uses: actions/xgj/npm-install@main
# 测试1: 版本文件包含 v 前缀
- name: 创建带 v 前缀的版本文件
run: echo "v1.2.3" > /tmp/version-with-v
- name: 发布(版本文件带 v 前缀)
id: test1
uses: actions/xgj/release-web@main
with:
release-command: "echo 'Test release with v prefix in file'"
version-file: "/tmp/version-with-v"
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: 验证版本输出(不应包含 v
run: |
echo "版本输出: ${{ steps.test1.outputs.version }}"
if [[ "${{ steps.test1.outputs.version }}" == "1.2.3" ]]; then
echo "✅ 正确:版本文件 v 前缀已去除"
else
echo "❌ 错误:版本应该是 1.2.3,实际是 ${{ steps.test1.outputs.version }}"
exit 1
fi
# 测试2: Git tag 包含 v 前缀
- name: 创建带 v 前缀的 git tag
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git tag v1.2.4
# 删除版本文件,强制使用 git tag
rm -f /tmp/last-version /tmp/version-with-v
- name: 发布(基于 git tag
id: test2
uses: actions/xgj/release-web@main
with:
release-command: "echo 'Test release with git tag'"
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: 验证 git tag 版本输出
run: |
echo "Git tag 版本输出: ${{ steps.test2.outputs.version }}"
if [[ "${{ steps.test2.outputs.version }}" == "1.2.4" ]]; then
echo "✅ 正确Git tag v 前缀已去除"
else
echo "❌ 错误:版本应该是 1.2.4,实际是 ${{ steps.test2.outputs.version }}"
exit 1
fi
# 测试3: package.json 包含 v 前缀(不常见但要处理)
- name: 修改 package.json 添加 v 前缀
run: |
# 创建一个测试用的 package.json
echo '{"version": "v1.2.5"}' > package.json
rm -f /tmp/last-version
git tag -d v1.2.4 # 删除之前的 tag
- name: 发布(基于 package.json
id: test3
uses: actions/xgj/release-web@main
with:
release-command: "echo 'Test release with package.json'"
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: 验证 package.json 版本输出
run: |
echo "Package.json 版本输出: ${{ steps.test3.outputs.version }}"
if [[ "${{ steps.test3.outputs.version }}" == "1.2.5" ]]; then
echo "✅ 正确Package.json v 前缀已去除"
else
echo "❌ 错误:版本应该是 1.2.5,实际是 ${{ steps.test3.outputs.version }}"
exit 1
fi
# 测试4: 提交信息包含 v 前缀
- name: 创建带 v 前缀的提交信息
run: |
echo '{"version": "0.0.1"}' > package.json # 重置 package.json
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add package.json
git commit --allow-empty -m "release: v1.2.6"
- name: 发布(基于提交信息)
id: test4
uses: actions/xgj/release-web@main
with:
release-command: "echo 'Test release with commit message'"
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
- name: 验证提交信息版本输出
run: |
echo "提交信息版本输出: ${{ steps.test4.outputs.version }}"
if [[ "${{ steps.test4.outputs.version }}" == "1.2.6" ]]; then
echo "✅ 正确:提交信息 v 前缀已去除"
else
echo "❌ 错误:版本应该是 1.2.6,实际是 ${{ steps.test4.outputs.version }}"
exit 1
fi
# 测试5: 版本号带横线格式
- name: 验证版本带横线格式
run: |
echo "版本带横线: ${{ steps.test4.outputs.version-with-dash }}"
if [[ "${{ steps.test4.outputs.version-with-dash }}" == "1-2-6" ]]; then
echo "✅ 正确:版本横线格式正确"
else
echo "❌ 错误:版本横线格式应该是 1-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 "✅ 版本横线格式正确"