Azure Pipeline中DotNet Pack步骤是否包含Restore操作?如何优化构建耗时
Great question—this is a common optimization point for .NET pipelines, so let’s break this down clearly for you.
Short Answer
Yes, the pack command in the DotNetCoreCLI@2 task automatically runs a restore by default if your project dependencies haven’t already been resolved. You can safely remove your standalone DotNet - Restore task to eliminate redundant work and cut down your pipeline runtime.
Why You’re Seeing Duplicate Work
The native .NET CLI dotnet pack command includes implicit restore logic: if your project’s dependencies aren’t already present (from a prior restore or build), it will automatically trigger dotnet restore before proceeding to package your project. The Azure Pipeline task is just a wrapper around this CLI behavior, which explains why both your Restore and Pack steps were performing identical dependency resolution and build operations.
Optimized Pipeline Configuration
Here’s how to adjust your YAML to remove the duplicate restore while retaining your original settings:
steps: - task: DotNetCoreCLI@2 displayName: 'DotNet - Pack' inputs: command: 'pack' packagesToPack: '**/*.csproj' versioningScheme: 'off' noCache: true # Keeps your original "no cache" restore behavior - task: DotNetCoreCLI@2 displayName: 'DotNet - Push' inputs: command: 'push' packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg' nuGetFeedType: 'internal' publishVstsFeed: 'my-feed'
Additional Tips
- If you ever need to explicitly skip the restore in the Pack step (for example, if you’re restoring dependencies via a custom mechanism), you can add
restore: falseto the Pack task’s inputs. But for your use case, letting Pack handle the restore directly is the cleanest, most efficient approach. - This change should significantly reduce your pipeline runtime by removing the redundant restore and build operations that were running twice.
内容的提问来源于stack exchange,提问作者Nayden Van

