Visual Studio编译正常但GitHub Actions构建失败(SSDT相关)
问题描述
本地Visual Studio中重建项目可完全成功,但通过GitHub Actions构建时出现以下问题:
- 使用
dotnet build命令时,WebAppDB.sqlproj报MSB4019错误,提示找不到SSDT的Microsoft.Data.Tools.Schema.SqlTasks.targets文件 - 切换到MSBuild构建后,SSDT问题未解决,还新增MSB4078警告,WebApp和Unit Tests项目不被当前MSBuild版本支持
本地Visual Studio重建日志
Rebuild started at 2:29 PM... 1>------ Rebuild All started: Project: WebAppDB, Configuration: Debug Any CPU ------ 2>------ Rebuild All started: Project: WebApp, Configuration: Debug Any CPU ------ 1> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /nowarn:1701,1702,2008 /fullpaths /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll" /debug+ /debug:full /optimize- /out:obj\Debug\WebAppDB.dll /subsystemversion:6.00 /target:library /warnaserror- /utf8output /langversion:7.3 "C:\Users\seisc\AppData\Local\Temp\.NETFramework,Version=v4.7.2.SqlClrAttributes.cs" Restored C:\Users\seisc\Source\Repos\capstone_project\WebApp\WebApp.csproj (in 102 ms). Restored C:\Users\seisc\Source\Repos\capstone_project\Unit Tests\Unit Tests.csproj (in 124 ms). 1> Loading project references... 1> Loading project files... 1> Building the project model and resolving object interdependencies... 1> Validating the project model... 1> Writing model to C:\Users\seisc\Source\Repos\capstone_project\WebAppDB\obj\Debug\Model.xml... 1> WebAppDB -> C:\Users\seisc\Source\Repos\capstone_project\WebAppDB\bin\Debug\WebAppDB.dll 1> WebAppDB -> C:\Users\seisc\Source\Repos\capstone_project\WebAppDB\bin\Debug\WebAppDB.dacpac 2>C:\Users\seisc\Source\Repos\capstone_project\WebApp\Views\Register\Register.cshtml(22,15,22,26): warning CS8602: Dereference of a possibly null reference. 2>C:\Users\seisc\Source\Repos\capstone_project\WebApp\Models\VoterModel.cs(64,13,64,18): warning CS8602: Dereference of a possibly null reference. 2>C:\Users\seisc\Source\Repos\capstone_project\WebApp\Models\VoterModel.cs(121,17,121,22): warning CS8602: Dereference of a possibly null reference. 2>WebApp -> C:\Users\seisc\Source\Repos\capstone_project\WebApp\bin\Debug\net8.0\WebApp.dll 2>Done building project "WebApp.csproj". 3>------ Rebuild All started: Project: Unit Tests, Configuration: Debug Any CPU ------ 3>Unit Tests -> C:\Users\seisc\Source\Repos\capstone_project\Unit Tests\bin\Debug\net8.0\Unit Tests.dll ========== Rebuild All: 3 succeeded, 0 failed, 0 skipped ========== ========== Rebuild completed at 2:30 PM and took 02.043 seconds ==========
GitHub Actions构建日志(dotnet build)
Run dotnet build --no-restore MSBuild version 17.9.8+b34f75857 for .NET D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj(56,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\8.0.204\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\8.0.204\\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" is correct, and that the file exists on disk. Warning: D:\a\capstone_project\capstone_project\WebApp\Models\VoterModel.cs(64,13): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] Warning: D:\a\capstone_project\capstone_project\WebApp\Models\VoterModel.cs(121,17): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] Warning: D:\a\capstone_project\capstone_project\WebApp\Views\Register\Register.cshtml(22,15): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] WebApp -> D:\a\capstone_project\capstone_project\WebApp\bin\Debug\net8.0\WebApp.dll Unit Tests -> D:\a\capstone_project\capstone_project\Unit Tests\bin\Debug\net8.0\Unit Tests.dll Build FAILED. Warning: D:\a\capstone_project\capstone_project\WebApp\Models\VoterModel.cs(64,13): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] Warning: D:\a\capstone_project\capstone_project\WebApp\Models\VoterModel.cs(121,17): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] Warning: D:\a\capstone_project\capstone_project\WebApp\Views\Register\Register.cshtml(22,15): warning CS8602: Dereference of a possibly null reference. [D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj] D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj(56,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\8.0.204\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\8.0.204\\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" is correct, and that the file exists on disk. 3 Warning(s) 1 Error(s) Time Elapsed 00:00:40.97 Error: Process completed with exit code 1.
GitHub Actions构建日志(MSBuild)
Run C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe capstone_project.sln /p:Configuration=Release Microsoft (R) Build Engine version 4.8.9037.0 [Microsoft .NET Framework, version 4.0.30319.42000] Copyright (C) Microsoft Corporation. All rights reserved. Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. Build started 5/1/2024 8:48:25 PM. Project "D:\a\capstone_project\capstone_project\capstone_project.sln" on node 1 (default targets). ValidateSolutionConfiguration: Building solution configuration "Release|Any CPU". Project "D:\a\capstone_project\capstone_project\capstone_project.sln" (1) is building "D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj.metaproj" (2) on node 1 (default targets). D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj.metaproj : warning MSB4078: The project file "WebApp\WebApp.csproj" is not supported by MSBuild and cannot be built. Done Building Project "D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj.metaproj" (default targets). Project "D:\a\capstone_project\capstone_project\capstone_project.sln" (1) is building "D:\a\capstone_project\capstone_project\Unit Tests\Unit Tests.csproj.metaproj" (3) on node 1 (default targets). D:\a\capstone_project\capstone_project\Unit Tests\Unit Tests.csproj.metaproj : warning MSB4078: The project file "Unit Tests\Unit Tests.csproj" is not supported by MSBuild and cannot be built. Done Building Project "D:\a\capstone_project\capstone_project\Unit Tests\Unit Tests.csproj.metaproj" (default targets). Project "D:\a\capstone_project\capstone_project\capstone_project.sln" (1) is building "D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj" (4) on node 1 (default targets). D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj(57,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. Done Building Project "D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj" (default targets) -- FAILED. Done Building Project "D:\a\capstone_project\capstone_project\capstone_project.sln" (default targets) -- FAILED. Build FAILED. "D:\a\capstone_project\capstone_project\capstone_project.sln" (default target) (1) -> "D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj.metaproj" (default target) (2) -> (Build target) -> D:\a\capstone_project\capstone_project\WebApp\WebApp.csproj.metaproj : warning MSB4078: The project file "WebApp\WebApp.csproj" is not supported by MSBuild and cannot be built. "D:\a\capstone_project\capstone_project\capstone_project.sln" (default target) (1) -> "D:\a\capstone_project\capstone_project\Unit Tests\Unit Tests.csproj.metaproj" (default target) (3) -> D:\a\capstone_project\capstone_project\Unit Tests\Unit Tests.csproj.metaproj : warning MSB4078: The project file "Unit Tests\Unit Tests.csproj" is not supported by MSBuild and cannot be built. "D:\a\capstone_project\capstone_project\capstone_project.sln" (default target) (1) -> "D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj" (default target) (4) -> D:\a\capstone_project\capstone_project\WebAppDB\WebAppDB.sqlproj(57,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. 2 Warning(s) 1 Error(s) Time Elapsed 00:00:02.38 Error: Process completed with exit code 1.
当前dotnet.yml工作流配置
# This workflow will build a .NET project # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net name: .NET on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Setup .NET uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore - name: Build with MSBuild run: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe capstone_project.sln /p:Configuration=Release - name: Test run: dotnet test --no-build --verbosity normal
解决方案
1. 安装SSDT构建工具
GitHub Actions的Windows runner默认未预装SSDT,需要在工作流中添加安装步骤,使用Chocolatey包管理器安装:
- name: Install SSDT run: choco install sql-server-data-tools -y
2. 使用兼容.NET 8的MSBuild版本
不要使用.NET Framework 4.8自带的老旧MSBuild(路径C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe),该版本不支持.NET 8项目。通过官方action获取Visual Studio附带的最新MSBuild:
- name: Setup MSBuild uses: microsoft/setup-msbuild@v2
之后直接使用msbuild命令即可调用正确版本。
3. 完整修改后的工作流配置
整合以上步骤,更新后的dotnet.yml如下:
# This workflow will build a .NET project name: .NET on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: Setup .NET uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - name: Install SSDT run: choco install sql-server-data-tools -y - name: Setup MSBuild uses: microsoft/setup-msbuild@v2 - name: Restore dependencies run: dotnet restore - name: Build with MSBuild run: msbuild capstone_project.sln /p:Configuration=Release /m - name: Test run: dotnet test --no-build --verbosity normal
4. 可选:更新SqlProj的目标文件路径
如果WebAppDB.sqlproj是旧版本,可修改其导入的目标文件路径,确保适配最新SSDT版本。打开WebAppDB.sqlproj,找到Import节点,替换为:
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')" />
内容的提问来源于stack exchange,提问作者Shane Salter
相关产品推荐
相关产品推荐

