GeWuYou dee89e1134 feat(ci): 更新CI工作流以支持多版本.NET SDK
- 添加.NET 9和.NET 10版本的支持
- 将原有的.NET SDK设置步骤重命名为.NET 8
- 配置多个.NET版本以进行兼容性测试
- 更新actions/setup-dotnet到v5版本
- 优化SDK安装流程以提高构建效率
2026-01-29 19:27:46 +08:00

110 lines
3.4 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CI/CD工作流配置构建和测试.NET项目
# 该工作流在push到main/master分支或创建pull request时触发
name: CI - Build & Test
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
permissions:
contents: read
jobs:
test:
name: Build and Test
runs-on: ubuntu-latest
steps:
# 检出源代码设置fetch-depth为0以获取完整的git历史
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
# TruffleHog OSS 扫描步骤
# 使用 TruffleHog 工具扫描代码库中的敏感信息泄露如API密钥、密码等
# 该步骤会比较基础分支和当前提交之间的差异,检测新增内容中是否包含敏感数据
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@v3.92.5
with:
# 扫描路径,. 表示扫描整个仓库
path: .
# 基础提交哈希,用于与当前提交进行比较
base: ${{ github.event.before }}
# 当前提交哈希,作为扫描的目标版本
head: ${{ github.sha }}
# 安装和配置.NET SDK版本
- name: Setup .NET 8
uses: actions/setup-dotnet@v5
with:
dotnet-version: 8.0.x
- name: Setup .NET 9
uses: actions/setup-dotnet@v5
with:
dotnet-version: 9.0.x
- name: Setup .NET 10
uses: actions/setup-dotnet@v5
with:
dotnet-version: 10.0.x
# 配置NuGet包缓存以加速后续构建
- name: Cache NuGet packages
uses: actions/cache@v5
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
# 执行NuGet包恢复操作
- name: Restore
run: dotnet restore
# 恢复.NET本地工具
- name: Restore .NET tools
run: dotnet tool restore
# 构建项目使用Release配置且跳过恢复步骤
- name: Build
run: dotnet build -c Release --no-restore
# 运行单元测试输出TRX格式结果到TestResults目录
- name: Test
run: |
dotnet test \
-c Release \
--no-build \
--logger "trx;LogFileName=testResults-$(date +%s).trx" \
--results-directory TestResults
- name: Generate CTRF report
run: |
latest=$(ls -t TestResults/*.trx | head -n 1)
dotnet tool run DotnetCtrfJsonReporter -p "$latest" -t nunit
- name: Debug - Verify CTRF files
if: always()
run: |
echo "Checking for CTRF files..."
find . -name "*.json" -path "*/ctrf/*" -ls
ls -la ctrf/ 2>/dev/null || echo "No ctrf directory found"
# 生成并发布测试报告,无论测试成功或失败都会执行
- name: Test Report
uses: dorny/test-reporter@v2
if: always()
with:
name: .NET Test Results
path: TestResults/*.trx
reporter: dotnet-trx
- name: Publish Test Report
uses: ctrf-io/github-test-reporter@v1
with:
report-path: './ctrf/*.json'
github-report: true
pull-request-report: true
summary-delta-report: true
insights-report: true
flaky-rate-report: true
fail-rate-report: true
slowest-report: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: always()