mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 16:53:37 +08:00
feat: 增强 Web 项目发布构建的 GitHub Action,添加自定义版本文件路径支持,优化版本获取逻辑,确保版本号格式一致,更新文档以反映新功能和使用示例。
This commit is contained in:
73
release-web/examples/custom-version-source.yml
Normal file
73
release-web/examples/custom-version-source.yml
Normal file
@@ -0,0 +1,73 @@
|
||||
name: 自定义版本源示例
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # 需要完整历史记录以获取 git tags
|
||||
|
||||
- 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: 使用自定义版本文件路径
|
||||
- name: 发布(自定义版本文件)
|
||||
uses: actions/xgj/release-web@main
|
||||
with:
|
||||
release-command: "echo '1.2.3' > ./custom-version.txt"
|
||||
version-file: "./custom-version.txt"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
# 情况2: 依赖 git tag(无版本文件)
|
||||
- name: 创建 git tag
|
||||
run: |
|
||||
git config user.name "github-actions"
|
||||
git config user.email "github-actions@github.com"
|
||||
git tag v1.2.4
|
||||
|
||||
- name: 发布(基于 git tag)
|
||||
uses: actions/xgj/release-web@main
|
||||
with:
|
||||
release-command: "echo 'Release without version file'"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
# 情况3: 依赖 package.json(推荐用于 Node.js 项目)
|
||||
- name: 更新 package.json 版本
|
||||
run: npm version 1.2.5 --no-git-tag-version
|
||||
|
||||
- name: 发布(基于 package.json)
|
||||
uses: actions/xgj/release-web@main
|
||||
with:
|
||||
release-command: "npm run build" # 不生成版本文件
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
# 情况4: 基于提交信息
|
||||
- name: 提交版本信息
|
||||
run: |
|
||||
git config user.name "github-actions"
|
||||
git config user.email "github-actions@github.com"
|
||||
git commit --allow-empty -m "release: 1.2.6"
|
||||
|
||||
- name: 发布(基于提交信息)
|
||||
uses: actions/xgj/release-web@main
|
||||
with:
|
||||
release-command: "echo 'Release from commit message'"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
148
release-web/examples/version-handling.yml
Normal file
148
release-web/examples/version-handling.yml
Normal 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 "✅ 版本横线格式正确"
|
Reference in New Issue
Block a user