[英]Nuget restore fails on Azure Devops with message "unable to load the service index for source"
I have a build for a .NET solution that is running in a private agent.我有一个在私有代理中运行的 .NET 解决方案的构建。 The solution contains both .NET Core 2.1 and .NET Standard 2.0 projects.
该解决方案包含 .NET Core 2.1 和 .NET Standard 2.0 项目。
Some of the nuget packages installed are the following:安装的一些 nuget 包如下:
The build fails when trying to restore the nuget packages with the following error:尝试恢复 nuget 包时构建失败,并出现以下错误:
"F:\Agent01\w\141\s\xxxxxxx.sln" (Restore target) (1) -> (Restore target) -> C:\Program Files\dotnet\sdk\2.1.500\NuGet.targets(114,5): error: Unable to load the service index for source https://xxxxxxxxxx.pkgs.visualstudio.com/_packaging/xxxxxxxxxx/nuget/v3/index.json .
"F:\Agent01\w\141\s\xxxxxxx.sln" (恢复目标) (1) -> (恢复目标) -> C:\Program Files\dotnet\sdk\2.1.500\NuGet.targets(114 ,5): 错误:无法加载源https://xxxxxxxxxx.pkgs.visualstudio.com/_packaging/xxxxxxxxxx/nuget/v3/index.json的服务索引。 C:\Program Files\dotnet\sdk\2.1.500\NuGet.targets(114,5): error: Response status code does not indicate success: 401 (Unauthorized).
C:\Program Files\dotnet\sdk\2.1.500\NuGet.targets(114,5):错误:响应状态码不表示成功:401(未授权)。
Build task is the following:构建任务如下:
This is the content of %appdata%\NuGet\nuget.config
file in the build agent:这是构建代理中
%appdata%\NuGet\nuget.config
文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="MyFeed" value="https://xxxxxxxxxx.pkgs.visualstudio.com/_packaging/xxxxxxxxxx/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<MyFeed>
<add key="Username" value="LocalBuildAgent" />
<add key="ClearTextPassword" value="xxxxxxxxxxx" />
</MyFeed>
</packageSourceCredentials>
</configuration>
I already checked a few similar questions but so far I wasn't able to find a solution for my problem.我已经检查了一些类似的问题,但到目前为止我无法找到解决我的问题的方法。
Some notes:一些注意事项:
What am I missing?我错过了什么? How to fix this issue?
如何解决这个问题? Why can't I restore the packages using the
dotnet restore
command?为什么我不能使用
dotnet restore
命令恢复包?
Packages are restored without errors when using the old Nuget Restore task as follows:使用旧的Nuget 恢复任务时,包恢复没有错误,如下所示:
I am able to restore the packages using the .NET Core task v1:我可以使用 .NET 核心任务 v1 恢复包:
Or using v2 task with argument --force
:或使用带有参数的 v2 任务
--force
:
I found a solution - add the following package source to %appdata%\\NuGet\\nuget.config
:我找到了一个解决方案 - 将以下包源添加到
%appdata%\\NuGet\\nuget.config
:
<add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
Complete file looks like this:完整的文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="MyFeed" value="https://xxxxxxxxxx.pkgs.visualstudio.com/_packaging/xxxxxxxxxx/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<MyFeed>
<add key="Username" value="LocalBuildAgent" />
<add key="ClearTextPassword" value="xxxxxxxxxxx" />
</MyFeed>
</packageSourceCredentials>
</configuration>
Also, check Regression in .NET SDK 500: 'dotnet tool install' fails with 401 (Unauthorized) when there is a private feed in NuGet.config #7524 .此外,请检查.NET SDK 500 中的回归:当 NuGet.config #7524 中存在私有源时,'dotnet tool install' 失败并显示 401(未经授权) 。 This problem seems to be caused by .NET SDK 2.1.500.
此问题似乎是由 .NET SDK 2.1.500 引起的。
Another workaround would be to uninstall that version:另一种解决方法是卸载该版本:
The issue is not present in .NET Core SDK 2.1.400, eg it goes away when .NET Core SDK 2.1.500 is uninstalled, and reappears when SDK 2.1.500 is installed again.
该问题在 .NET Core SDK 2.1.400 中不存在,例如,当卸载 .NET Core SDK 2.1.500 时它会消失,并在再次安装 SDK 2.1.500 时再次出现。
Using the latest "Use .NET Core sdk 2.1.504" task worked for me.使用最新的“使用 .NET Core sdk 2.1.504”任务对我有用。 Seems there are some buggy versions of .NET Core sdk 2.1.5xx out there.
似乎有一些有问题的 .NET Core sdk 2.1.5xx 版本。
I encountered the same issue but for a different reason - not having granted the PAT the appropriate access flags.我遇到了同样的问题,但出于不同的原因 - 没有授予 PAT 适当的访问标志。 The Packaging (Create, read, update, and delete feeds and packages) scope is required for the PAT, I had before only set the PAT to have a scope of Build (Artifacts, definitions, requests, queue a build, and updated build properties) having mistaken Artifacts as including private package feeds!
PAT 需要打包(创建、读取、更新和删除提要和包)范围,我之前只将 PAT 设置为具有构建范围(工件、定义、请求、构建队列和更新的构建属性) ) 将 Artifacts 误认为包括私有包提要!
The user experience in VS (both 2015 and 2017) wasn't at all helpful though, both versions repeatedly popping-up the credentials dialog instead of giving more information about what the reason might be (apart from the 401 error response, the clue being in the 'Unauthorized' word though ...). VS(2015 年和 2017 年)中的用户体验根本没有帮助,两个版本都反复弹出凭据对话框,而不是提供有关原因可能是什么的更多信息(除了 401 错误响应,线索是虽然在“未经授权”这个词中......)。
To summarize the steps to consume a private DevOps package feed -总结使用私有 DevOps 包提要的步骤 -
Add the package source (with credentials) to your %APPDATA%\\NuGet\\NuGet.config using -使用 - 将包源(带有凭据)添加到您的 %APPDATA%\\NuGet\\NuGet.config
nuget.exe sources add -name {your_package_feed_name} -source https://pkgs.dev.azure.com/{your_org}/_packaging/{your_feed}/nuget/v3/index.json -username PATForPackages -password {the_pat_value_you_got_from_azure_devops}
Note: nuget sources add will Base-64 encode the PAT into the packageSourceCredentials Password setting.注意:nuget 源添加会将 PAT 进行 Base-64 编码到 packageSourceCredentials Password 设置中。 Also being in your user profile the NuGet.config file is relatively secure provided you keep it secured there, the downside is that this is a host pre-requisite, a consequence of nuget not having in-built Azure DevOps authentication.
同样在您的用户配置文件中,NuGet.config 文件相对安全,前提是您将其保护在那里,但缺点是这是主机先决条件,这是 nuget 没有内置 Azure DevOps 身份验证的结果。
For thoses comming here in 2021 for the same error message, adding the NuGetAuthenticate@0 task before the pack command may be the missing piece:对于那些在 2021 年因相同错误消息来到这里的人,在 pack 命令之前添加 NuGetAuthenticate@0 任务可能是缺失的部分:
- task: NuGetAuthenticate@0
- task: DotNetCoreCLI@2
inputs:
command: 'pack'
packagesToPack: $(projectPath)
includesymbols: true
includesource: true
versioningScheme: 'off'
verbosityPack: Normal
displayName: 'Dotnet Pack'
Here are the steps that helped me fix this same issue in Visual Studio when I was trying to build a solution that was using a NuGet package hosted in Azure DevOps.当我尝试构建使用 Azure DevOps 中托管的 NuGet package 的解决方案时,以下是帮助我在 Visual Studio 中解决相同问题的步骤。
Hopefully this fix your issue too!希望这也能解决您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.