Azure DevOps中.NET 5构建管道执行dotnet build时超时被取消
Azure DevOps .NET 5构建管道超时问题排查
我在Azure DevOps中配置了一个用于构建.NET 5解决方案的构建管道,此前运行正常,但最近在构建阶段因超时失败,dotnet build命令出现卡顿。
构建日志
2024-03-18T10:53:27.4401495Z ##[section]Starting: Buid solution 2024-03-18T10:53:27.4525992Z ============================================================================== 2024-03-18T10:53:27.4526134Z Task : .NET Core 2024-03-18T10:53:27.4526208Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command 2024-03-18T10:53:27.4526329Z Version : 2.235.1 2024-03-18T10:53:27.4526385Z Author : Microsoft Corporation 2024-03-18T10:53:27.4526457Z Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli 2024-03-18T10:53:27.4526581Z ============================================================================== 2024-03-18T10:53:27.8552527Z [command]C:\Windows\system32\chcp.com 65001 2024-03-18T10:53:27.8655298Z Active code page: 65001 2024-03-18T10:53:27.8684055Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version. 2024-03-18T10:53:28.3062787Z [command]C:\hostedtoolcache\windows\dotnet\dotnet.exe build D:\a\1\s\src\MES.CloudApplication.sln "-dl:CentralLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.235.1\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"*ForwardingLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.235.1\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"" 2024-03-18T10:53:28.5921283Z Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET 2024-03-18T10:53:28.5921873Z Copyright (C) Microsoft Corporation. All rights reserved. 2024-03-18T10:53:28.5922177Z 2024-03-18T10:53:29.2303494Z Determining projects to restore... 2024-03-18T10:53:30.1778985Z Restored D:\a\1\s\tests\MES.CloudApplication.Infrastructure.Tests\MES.CloudApplication.Infrastructure.Tests.csproj (in 624 ms). 2024-03-18T10:53:30.1779760Z Restored D:\a\1\s\tests\MES.CloudApplication.Application.Tests\MES.CloudApplication.Application.Tests.csproj (in 512 ms). 2024-03-18T10:53:30.2894980Z Restored D:\a\1\s\src\MES.CloudApplication.Workflow\MES.CloudApplication.Workflow.csproj (in 83 ms). 2024-03-18T10:53:30.3039944Z Restored D:\a\1\s\tests\MES.CloudApplication.Api.Tests\MES.CloudApplication.Api.Tests.csproj (in 99 ms). 2024-03-18T10:53:30.5759744Z Restored D:\a\1\s\src\MES.CloudApplication.Printing\MES.CloudApplication.Printing.csproj (in 183 ms). 2024-03-18T10:53:30.5760975Z Restored D:\a\1\s\src\MES.CloudApplication.Printing.Tests\MES.CloudApplication.Printing.Tests.csproj (in 235 ms). 2024-03-18T10:53:30.7334699Z Restored D:\a\1\s\src\MES.CloudApplication.Integration\MES.CloudApplication.Integration.csproj (in 216 ms). 2024-03-18T10:53:30.7335992Z Restored D:\a\1\s\src\MES.CloudApplication.Infrastructure\MES.CloudApplication.Infrastructure.csproj (in 54 ms). 2024-03-18T10:53:30.9119467Z Restored D:\a\1\s\src\MES.CloudApplication.Domain\MES.CloudApplication.Domain.csproj (in 12 ms). 2024-03-18T10:53:30.9120408Z Restored D:\a\1\s\src\MES.CloudApplication.Application\MES.CloudApplication.Application.csproj (in 175 ms). 2024-03-18T10:53:30.9720180Z Restored D:\a\1\s\src\MES.CloudApplication.Api\MES.CloudApplication.Api.csproj (in 218 ms). 2024-03-18T10:53:34.4069633Z MES.CloudApplication.Api.Tests -> D:\a\1\s\tests\MES.CloudApplication.Api.Tests\bin\Debug\net5.0\MES.CloudApplication.Api.Tests.dll 2024-03-18T10:53:34.5178908Z MES.CloudApplication.Domain -> D:\a\1\s\src\MES.CloudApplication.Domain\bin\Debug\net5.0\MES.CloudApplication.Domain.dll 2024-03-18T11:50:15.0053211Z ##[error]The Operation will be canceled. The next steps may not contain expected logs. 2024-03-18T11:50:24.6110630Z ##[error]The operation was canceled. 2024-03-18T11:50:24.6418805Z ##[section]Finishing: Buid solution
当前管道配置
pool: vmImage: 'windows-latest' variables: solution: '**/*.sln' buildPlatform: 'Any CPU' buildConfiguration: 'Release' steps: - task: UseDotNet@2 inputs: version: '5.0.x' includePreviewVersions: true # Required for preview versions displayName: Install sdk - task: DotNetCoreCLI@2 displayName: 'dotnet restore' inputs: command: 'restore' projects: '**/*.csproj' feedsToUse: 'config' nugetConfigPath: 'NuGet.config' - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/*.sln' displayName: Buid solution - task: DotNetCoreCLI@2 inputs: command: 'test' projects: '**/*Tests.csproj' arguments: '--configuration $(buildConfiguration) --no-build' displayName: Unit test - task: DotNetCoreCLI@2 inputs: command: 'publish' publishWebProjects: false projects: '**src/MES.CloudApplication.Api/*.csproj' arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)' zipAfterPublish: true displayName: Publish artifacts - task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'drop' displayName: Copy artifacts
注:除Api项目外,所有项目均为.NET 5类库。
排查与解决建议
- 指定Release构建配置:当前
build任务未指定构建配置,默认使用Debug模式构建。Debug模式包含更多调试符号和检查,会显著增加构建时间。修改build任务,添加arguments参数指定Release配置:- task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/*.sln' arguments: '--configuration $(buildConfiguration)' displayName: Build solution - 拆分构建任务:将解决方案中的类库和Api项目分开构建,避免单个
dotnet build命令同时处理过多项目,减少资源竞争:- task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/*.csproj' arguments: '--configuration $(buildConfiguration) --exclude **/MES.CloudApplication.Api.csproj' displayName: Build class libraries - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/MES.CloudApplication.Api.csproj' arguments: '--configuration $(buildConfiguration)' displayName: Build Api project - 切换特定版本的托管代理:
windows-latest镜像会自动更新,可能引入兼容性问题。尝试切换到固定版本的镜像(如windows-2022),保持环境稳定。 - 清理构建缓存:托管代理的NuGet缓存或构建输出可能存在损坏,添加清理步骤重置缓存:
- task: CmdLine@2 inputs: script: 'dotnet clean --configuration $(buildConfiguration) && dotnet nuget locals all --clear' displayName: Clean build cache and NuGet locals - 检查自定义MSBuild任务:如果项目中包含自定义的MSBuild目标(如代码生成、静态分析),临时禁用这些任务,确认是否是此类操作导致构建卡顿。
- 排查依赖变更:确认最近是否新增或更新了NuGet包,某些包可能在构建时触发耗时操作,尝试回滚到之前正常版本的依赖进行测试。
内容的提问来源于stack exchange,提问作者Mustafa
相关产品推荐
相关产品推荐

