在 .NET 10 中使用 C# 实现 CI 脚本
目录IntroSampleMoreReferencesIntro之前我们介绍了过一期基于 dotnet-exec 来实现 C# 脚本实现 CI 基于 C# 编写构建脚本.NET 10 SDK 支持了 dotnet run file 或者 file-based app 我们可以直接使用 dotnet run file 支持来实现了不熟悉 dotnet run file 的朋友可以参考之前的文章介绍你好 dotnet run file, 再见 csproj.NET 10 Preview 6 run file 的一些优化基于此我们可以进一步简化我们的 CI 脚本了之前需要额外安装一个 tool现在直接使用 .NET SDK 就可以了SampleBuild script samplebuild.cs:#:package WeihanLi.Common1.0.84 using WeihanLi.Common.Helpers; var solutionPath ./WeihanLi.Npoi.slnx; string[] srcProjects [ ./src/WeihanLi.Npoi/WeihanLi.Npoi.csproj ]; string[] testProjects [ ./test/WeihanLi.Npoi.Test/WeihanLi.Npoi.Test.csproj ]; await DotNetPackageBuildProcess .Create(options { options.SolutionPath solutionPath; options.SrcProjects srcProjects; options.TestProjects testProjects; }) .ExecuteAsync(args);这里 bash script 里引用自己写的一个 nuget 包并指定了使用1.0.84版本构建 nuget 包的逻辑定义在了这个 nuget 包里以实现复用和精简构建脚本所以实际的脚本里只指定了解决方案文件路径与源代码项目路径和测试项目路径感兴趣的朋友可以参考源码https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/BuildProcess.csGitHub Actions workflow sample:name: default on: push jobs: windows-build: runs-on: windows-latest steps: - uses: actions/checkoutv5 - name: Setup .NET SDK uses: actions/setup-dotnetv5 with: dotnet-version: 10.0.x - name: build run: dotnet build.cs这里构建脚本我们可以直接使用dotnet build.cs即可如果有参数要指定可以指定跟在后面比如dotnet build.cs --targetbuildrun sample:run-sample从 GitHub Action 记录里可以看到实际运行的效果示例https://github.com/WeihanLi/WeihanLi.Npoi/actions/runs/19406672198/job/55522318148再看一个 Azure DevOps 的示例和 GitHub Actions 是一样的trigger: branches: include: - * # must quote since * is a YAML reserved character; we want a string paths: exclude: - *.md pool: vmImage: windows-latest steps: - task: UseDotNet2 displayName: Use .NET SDK inputs: packageType: sdk version: 10.0.x - powershell: dotnet build.cs displayName: Powershell Script env: NuGet__ApiKey: $(nugetApiKey) NuGet__Source: $(nugetSourceUrl)这里指定了 powershell 也可以不指定使用默认的方式即可More在之前使用 dotnet-exec 来执行时为了不重复安装 dotnet tool 会在入口脚本build.ps1和build.sh里安装 dotnet tool因为平台差异需要指定两种脚本build.ps1给 Windows,build.sh给 Linux/Mac 用build.ps1:dotnet tool install -g dotnet-execute Write-Host dotnet-exec ./build/build.cs --args$ARGS -ForegroundColor GREEN dotnet-exec ./build/build.cs --args $ARGSbuild.sh:#!/bin/sh dotnet tool install -g dotnet-execute --prerelease export PATH$PATH:$HOME/.dotnet/tools echo dotnet-exec ./build/build.cs --args $ dotnet-exec ./build/build.cs --args $Linux 下有时候可能 dotnet tool 的 path 会没有设置所以这里会再设置一下将 dotnet tools 的路径导出到 PATH 里以免执行 tool 命令的时候找不到 tool使用 dotnet run file 就可以直接使用dotnet命令即可没太多平台差异也不太需要每个平台建一个 script 作为脚本入口如果你也想尝试下 C# 来做 CI 不妨也来试试吧References• https://github.com/WeihanLi/WeihanLi.Common/commit/8444d55c144964636087d007e41de2bd5811342e• https://github.com/WeihanLi/WeihanLi.Npoi/commit/7896791822182d2cac8a73fea31ffd5bde01788c• https://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/.github/workflows/dotnet.yml• https://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/build.cs• https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Helpers/BuildProcess.cs• https://github.com/WeihanLi/WeihanLi.Npoi/actions/runs/19406672198/job/55522318148引入地址