refactor(workflow): 重构自动标签工作流并添加CI构建测试

- 将自动标签触发方式从直接push改为基于CI工作流完成状态
- 新增独立的CI构建测试工作流配置文件
- 简化自动标签逻辑,移除跳过关键字检查和版本解析冗余代码
- 优化Git标签创建和推送流程,统一配置用户名邮箱
- 集成.NET项目构建测试流程,包含依赖恢复、编译和单元测试
- 更新工作流权限配置和并发控制设置
This commit is contained in:
GeWuYou 2026-01-29 12:14:38 +08:00
parent 2f443087a4
commit ef655bbaf1
2 changed files with 83 additions and 107 deletions

View File

@ -1,144 +1,60 @@
name: Auto Increment Version and Tag
# 工作流触发条件配置
# 当向 main 或 master 分支推送代码时触发
# 或者当针对 main 或 master 的 PR 被合并关闭时触发
on:
push:
branches: [ main, master ]
tags-ignore:
- '*'
workflow_run:
workflows: ["CI - Build & Test"]
types:
- completed
concurrency:
group: auto-tag-main
cancel-in-progress: false
jobs:
auto-tag:
name: Auto Increment Version and Create Tag
if: >
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'main'
runs-on: ubuntu-latest
# 条件判断:仅在推送事件或已合并的 PR 关闭事件中执行
if: github.event_name == 'push'
permissions:
contents: write
steps:
# 步骤一:检出仓库代码
# 使用 actions/checkout 动作获取完整的 Git 历史用于查找已有标签
- name: Checkout code
uses: actions/checkout@v4
with:
lfs: true
fetch-depth: 0 # 获取全部历史记录以查找现有标签
fetch-depth: 0
persist-credentials: false
# 步骤二:检查是否需要跳过打标签操作
# 根据最新提交信息决定是否继续后续流程
- name: Check for skip keyword
id: check_skip
run: |
# 检查最近一次提交信息是否包含跳过关键词
LAST_COMMIT_MSG=$(git log -1 --pretty=format:"%B")
if [[ "$LAST_COMMIT_MSG" == *"[skip]"* ]] || [[ "$LAST_COMMIT_MSG" == *"[no tag]"* ]]; then
echo "skip_tag=true" >> $GITHUB_OUTPUT
echo "Skipping tag creation due to skip keyword in commit message"
else
echo "skip_tag=false" >> $GITHUB_OUTPUT
echo "No skip keyword found, proceeding with tag creation"
fi
echo "Last commit message: $LAST_COMMIT_MSG"
# 运行测试
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build -c Release --no-restore -p:DebugType=portable
- name: Run tests
run: |
dotnet test \
--no-build \
-c Release \
--logger "trx" \
--results-directory ${{ github.workspace }}/TestResults
- name: Test Report
uses: dorny/test-reporter@v2
if: always()
with:
name: .NET Test Results
path: ${{ github.workspace }}/TestResults/*.trx
reporter: dotnet-trx
# 步骤三:计算下一个版本号(若未被跳过)
# 自动解析当前最新标签并递增修订号生成新的语义化版本号
- name: Get next version
id: get_next_version
if: steps.check_skip.outputs.skip_tag == 'false'
id: version
run: |
# 获取最新的标签版本号,如果没有标签则默认为 0.0.0
LATEST_TAG=$(git tag --list "v*" --sort=-v:refname | head -n 1)
LATEST_TAG=${LATEST_TAG:-v0.0.0}
# 移除可能存在的 v 前缀
VERSION_NUM=${LATEST_TAG#v}
# 解析主版本号、次版本号和修订号
IFS=. read MAJOR MINOR PATCH <<< "$VERSION_NUM"
unset IFS
# 递增修订号
PATCH=$((PATCH + 1))
# 构造新版本号
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
NEW_TAG="v$NEW_VERSION"
echo "latest_tag=$LATEST_TAG"
echo "new_version=$NEW_VERSION"
echo "new_tag=$NEW_TAG"
echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT
VERSION=${LATEST_TAG#v}
IFS=. read MAJOR MINOR PATCH <<< "$VERSION"
PATCH=$((PATCH+1))
echo "new_tag=v$MAJOR.$MINOR.$PATCH" >> $GITHUB_OUTPUT
# 步骤四:创建并推送新标签到远程仓库(若未被跳过)
# 使用个人访问令牌(PAT)进行身份验证完成推送操作
- name: Create tag and push (using PAT)
- name: Create tag
if: steps.check_skip.outputs.skip_tag == 'false'
env:
PAT: ${{ secrets.PAT_TOKEN }}
REPO: ${{ github.repository }}
NEW_TAG: ${{ steps.get_next_version.outputs.new_tag }}
TAG: ${{ steps.version.outputs.new_tag }}
run: |
set -e
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
if git show-ref --tags --verify --quiet "refs/tags/$NEW_TAG"; then
echo "Tag $NEW_TAG already exists, skipping"
exit 0
fi
echo "Creating annotated tag $NEW_TAG"
git tag -a "$NEW_TAG" -m "Auto-generated tag: $NEW_TAG"
# 推送单个 tag使用 PAT 作为 HTTPS token
echo "Pushing tag $NEW_TAG to origin using PAT"
git push "https://x-access-token:${PAT}@github.com/${REPO}.git" "refs/tags/${NEW_TAG}"
# 步骤五:输出本次成功创建的新版本相关信息(若未被跳过)
- name: Print version info
if: steps.check_skip.outputs.skip_tag == 'false'
run: |
echo "Previous tag was: ${{ steps.get_next_version.outputs.latest_tag }}"
echo "New tag created: ${{ steps.get_next_version.outputs.new_tag }}"
echo "Version number: ${{ steps.get_next_version.outputs.new_version }}"
# 步骤六:输出跳过原因信息(如果检测到了跳过关键字)
- name: Print skip info
if: steps.check_skip.outputs.skip_tag == 'true'
run: |
echo "Tag creation skipped due to commit message containing skip keyword"
git config user.name "GitHub Action"
git config user.email "action@github.com"
git tag -a "$TAG" -m "Auto tag $TAG"
git push "https://x-access-token:${PAT}@github.com/${{ github.repository }}.git" "$TAG"

60
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,60 @@
# CI/CD工作流配置构建和测试.NET项目
# 该工作流在push到main/master分支或创建pull request时触发
name: CI - Build & Test
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
name: Build and Test
runs-on: ubuntu-latest
steps:
# 检出源代码设置fetch-depth为0以获取完整的git历史
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
# 安装和配置.NET SDK版本9.0.x
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
# 配置NuGet包缓存以加速后续构建
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
# 执行NuGet包恢复操作
- name: Restore
run: dotnet restore
# 构建项目使用Release配置且跳过恢复步骤
- name: Build
run: dotnet build -c Release --no-restore
# 运行单元测试输出TRX格式结果到TestResults目录
- name: Test
run: |
dotnet test \
-c Release \
--no-build \
--logger trx \
--results-directory TestResults
# 生成并发布测试报告,无论测试成功或失败都会执行
- name: Test Report
uses: dorny/test-reporter@v2
if: always()
with:
name: .NET Test Results
path: TestResults/*.trx
reporter: dotnet-trx