背景
我正在对新建项目的构建管道进行故障排除。之前一切正常,最近我添加了一个来自相同内部 Artifacts Feed 的 nuget 包。为了确保找到所有 nuget 源[或者这就是希望],我正在使用 nuget.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
<add key="my-feed" value="https://pkgs.dev.azure.com/my-feed/_packaging/my-feed/nuget/v3/index.json" />
</packageSources>
</configuration>
它用于 dotnet 恢复,如下所示:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'
feedsToUse: 'config'
nugetConfigPath: './nuget.config'
verbosityRestore: 'Normal'
工作顺利。
我遇到的问题
下一步是运行我的单元测试,它已经这样设置了(我还没碰过):
- task: DotNetCoreCLI@2
displayName: 'Run Tests'
inputs:
command: 'test'
projects: '**/*.tests*/*.csproj'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
publishTestResults: true
运行时日志显示以下内容:
C:\Windows\system32\chcp.com 65001
Active code page: 65001
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.
"C:\Program Files\dotnet\dotnet.exe" test D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj --logger trx --results-directory D:\a\_temp --configuration Release "--collect:XPlat Code Coverage"
Determining projects to restore...
Restored D:\a\1\s\MyProject.Domain\MyProject.Domain.csproj (in 80 ms).
D:\a\1\s\MyProject.Contracts\MyProject.Contracts.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/my-feed-mes/_packaging/my-feed-mes/nuget/v3/index.json. [D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj]
D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/my-feed-mes/_packaging/my-feed-mes/nuget/v3/index.json.
Failed to restore D:\a\1\s\MyProject.Contracts\MyProject.Contracts.csproj (in 7.73 sec).
Failed to restore D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj (in 7.83 sec).
D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/my-feed/_packaging/my-feed/nuget/v3/index.json.
D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/my-feed/_packaging/my-feed/nuget/v3/index.json.
D:\a\1\s\MyProject.Tests\MyProject.Tests.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/my-feed/_packaging/my-feed/nuget/v3/index.json.
最终它继续运行,我不得不取消它(或者我猜它会在某个时候停止,但我等不了那么久)。
我的问题
可能导致此错误的原因 NU1301: 无法加载服务索引 源错误以及如何解决?
除了添加内部 nuget 包(并使用 nuget.config 来确保服务器上的源正确)之外,没有任何更改。
根据您的描述,dotnet恢复步骤可以成功恢复软件包,但dotnet测试步骤将显示NU1301错误。
问题的原因是 dotnet 恢复步骤将清除其使用的凭据。那么在 dotnet 测试步骤中无法读取有效凭据。
请参阅此文档:为什么我的构建、发布或测试步骤无法恢复包?
大多数 dotnet 命令(包括构建、发布和测试)都包含隐式恢复步骤。即使您在前面的步骤中成功运行了 dotnet 恢复,这也会对经过身份验证的提要失败,因为前面的步骤将清除它使用的凭据。
由于您已在管道中添加了 dotnet 恢复步骤,因此您可以在 dotnet 测试步骤中添加参数:
--no-restore
来跳过隐式恢复步骤。
例如:
- task: DotNetCoreCLI@2
displayName: 'Run Tests'
inputs:
command: 'test'
projects: '**/*.tests*/*.csproj'
arguments: '--no-restore --configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
publishTestResults: true
另一方面,由于单独的 dotnet 恢复步骤运行良好,我们可以确认项目中其他与 feed 相关的设置是正确的。