You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps Pipeline构建Azure Functions的.NET解决方案速度过慢问题

解决Azure Pipelines中.NET函数应用dotnet restore耗时过长的问题

问题核心

从日志能看出,虽然缓存命中,但实际缓存内容几乎为空,说明当前缓存配置仅捕获了packages.lock.json的哈希值,没有真正缓存NuGet包的下载目录,导致dotnet restore仍需重新处理依赖解析,完全没用到缓存加速。

优化方案

1. 修正缓存步骤的路径配置

必须明确指定要缓存的NuGet包存储目录,Azure Pipelines中可通过$(NUGET_PACKAGES)环境变量指向默认全局包缓存目录。修改后的Pipeline模板如下:

parameters:
  - name: is_debug_active
    type: boolean
    default: false
  - name: project_file_path
    type: string
  - name: build_configuration
    type: string

stages:
  - stage: Build
    jobs:
      - job:
        pool:
          vmImage: "windows-latest"

        steps:
          - ${{ each parameter in parameters }}:
              - script: |
                  echo ${{parameter.Key}} : ${{parameter.Value}}
                condition: eq(${{parameters.is_debug_active}}, True)

          # 新增NuGet缓存步骤,缓存实际包文件
          - task: Cache@2
            displayName: Cache NuGet packages
            inputs:
              key: 'nuget | "$(Agent.OS)" | **/packages.lock.json'
              restoreKeys: |
                nuget | "$(Agent.OS)"
                nuget
              path: $(NUGET_PACKAGES)

          - task: DotNetCoreCLI@2
            displayName: Building and publishing...
            inputs:
              command: "publish"
              projects: "${{parameters.project_file_path}}"
              arguments: "--configuration ${{parameters.build_configuration}} --output $(build.artifactstagingdirectory)"
              publishWebProjects: false
              zipAfterPublish: true

          - task: PublishPipelineArtifact@1
            inputs:
              targetPath: "$(build.artifactstagingdirectory)"
              artifactName: "drop"
              artifactType: "pipeline"

2. 拆分restore与publish步骤

默认dotnet publish会自动执行restore,拆分后可精准控制缓存时机,避免重复执行还原操作:

# 替换原有的publish步骤为以下两个独立步骤
- task: DotNetCoreCLI@2
  displayName: Restore dependencies
  inputs:
    command: "restore"
    projects: "${{parameters.project_file_path}}"
    feedsToUse: "select"
    # 若使用私有NuGet源,此处配置对应的源名称

- task: DotNetCoreCLI@2
  displayName: Build and publish
  inputs:
    command: "publish"
    projects: "${{parameters.project_file_path}}"
    arguments: "--configuration ${{parameters.build_configuration}} --output $(build.artifactstagingdirectory) --no-restore"
    publishWebProjects: false
    zipAfterPublish: true

3. 检查NuGet源配置

  • 确认Pipeline使用的NuGet源与本地Visual Studio一致,优先使用国内镜像或私有源加速下载
  • 添加脚本验证当前源配置:
- script: dotnet nuget list source
  displayName: Check NuGet sources

4. 验证缓存有效性

在缓存步骤后添加调试脚本,确认包缓存目录是否有内容:

- script: dir $(NUGET_PACKAGES)
  displayName: Verify cached packages
  condition: eq(${{parameters.is_debug_active}}, True)

关键提示

  • $(NUGET_PACKAGES)指向代理的NuGet全局缓存目录,确保缓存的是实际下载的包文件,而非仅锁定文件哈希
  • --no-restore参数可强制publish跳过自动还原,避免重复耗时操作

内容的提问来源于stack exchange,提问作者woozy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:15:35