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

Azure Pipeline构建正常但测试步骤无法找到私有NuGet包

问题:Azure Pipeline测试步骤无法找到私有NuGet包

我正在搭建用于部署应用的Azure Pipeline,希望先运行全部测试用例。目前构建和部署步骤均正常,但启用测试步骤后,出现无法找到私有Azure NuGet存储中my.nupack.Db包的错误,该包在构建步骤可正常获取。

错误信息

D:\a\1\s\myFolder\myProject.csproj : error NU1101: Unable to find package my.nupack.Db. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org [D:\a\1\s\myFolder.Test\myProject.Test.csproj]
  Failed to restore D:\a\1\s\myFolder\myProject.csproj (in 1.04 sec).
  Failed to restore D:\a\1\s\myFolder.Test\myProject.Test.csproj (in 1.09 sec).
  Restored D:\a\1\s\myFolder2\myProject2.csproj (in 8 ms).

当前Pipeline配置

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- main

variables:
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  projectNameFunctionApp: 'My.Project.Analysis'

    # Agent VM image name
  vmImageName: 'windows-latest'

  # Working Directory
  workingDirectory: '$(System.DefaultWorkingDirectory)'
  solution: '$(workingDirectory)/My.Project.sln'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - task: UseDotNet@2
      inputs:
        version: 6.0.x
    
    - task: NuGetToolInstaller@1
      inputs:
        versionSpec: 6.3.0
    - task: NuGetCommand@2
      displayName: 'NuGet restore'
      inputs:
        command: 'restore'
        restoreSolution: '**\*.sln'
        feedsToUse: 'select'
        vstsFeed: '{VSTS-UUID, I removed it}'
        
    - task: VSBuild@1
      displayName: Build
      inputs:
        solution: '**\*.sln'
        configuration: '$(buildConfiguration)'

    - task: DotNetCoreCLI@2
      displayName: Run Visual studio tests
      inputs:
        command: test
        projects: '**/*My.Project.Test*/*.csproj'
        arguments: '--configuration $(buildConfiguration)'

    - task: ArchiveFiles@2
      displayName: 'Archive $(projectNameFunctionApp)'
      inputs:
        rootFolderOrFile: "$(Build.SourcesDirectory)/$(projectNameFunctionApp)/bin/$(buildConfiguration)/net6.0"
        includeRootFolder: false
        archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"

    - publish: $(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip
      artifact: drop

备注:测试项目当前仅包含简单测试,并未依赖该包。


原因分析

测试项目虽未直接依赖my.nupack.Db,但它引用的主项目myProject.csproj需要该包。DotNetCoreCLI@2的test命令默认会自动触发项目还原,而这个步骤没有配置访问私有NuGet源的权限,只能从默认的nuget.org和Visual Studio离线源查找,因此找不到私有包。

解决方案

有两种可行方案:

方案1:在测试步骤中配置私有NuGet源

修改DotNetCoreCLI@2任务,添加私有源配置。如果使用Azure DevOps私有源,可直接通过任务参数指定:

- task: DotNetCoreCLI@2
  displayName: Run Visual studio tests
  inputs:
    command: test
    projects: '**/*My.Project.Test*/*.csproj'
    arguments: '--configuration $(buildConfiguration)'
    feedsToUse: 'select'
    vstsFeed: '{你的VSTS-UUID}'

也可以在arguments中直接指定源地址:

arguments: '--configuration $(buildConfiguration) --source https://pkgs.dev.azure.com/你的组织/_packaging/你的源名称/nuget/v3/index.json'

方案2:禁用测试步骤的自动还原

由于之前已经通过NuGetCommand@2完成了整个解决方案的包还原,测试步骤无需重复执行还原操作。在测试命令中添加--no-restore参数即可:

- task: DotNetCoreCLI@2
  displayName: Run Visual studio tests
  inputs:
    command: test
    projects: '**/*My.Project.Test*/*.csproj'
    arguments: '--configuration $(buildConfiguration) --no-restore'

该方案更高效,避免重复的还原操作。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 11:17:24