简体   繁体   English

Nuget 在 Azure Devops 上恢复失败,并显示消息“无法加载源的服务索引”

[英]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 包如下:

  • NETStandard.Library v2.0.3 NETStandard.Library v2.0.3
  • Microsoft.AspNetCore.Mvc v2.0.0 Microsoft.AspNetCore.Mvc v2.0.0
  • Microsoft.NETCore.App v2.1.5 Microsoft.NETCore.App v2.1.5

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:构建任务如下:

Nuget 恢复构建任务

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:一些注意事项:

  • Personal Access Token is NOT expired个人访问令牌未过期
  • This particular build runs successfully in other build agents此特定构建在其他构建代理中成功运行
  • There is at least 1 build with a " nuget restore " task that was run successfully using this agent (regular nuget restore task, NOT .NET Core)至少有 1 个带有“ nuget restore ”任务的构建使用此代理成功运行(常规nuget 恢复任务,不是 .NET 核心)
  • Tried restarting the build agent, without success尝试重新启动构建代理,但没有成功
  • Tried specifying a specific version of nuget before the restore, without success尝试恢复前指定特定版本的nuget ,没有成功
  • .NET Core SDK latest version in the build agent is 2.1.500 (multiple versions installed) .NET Core SDK 构建代理中最新版本为2.1.500(安装多个版本)

What am I missing?我错过了什么? How to fix this issue?如何解决这个问题? Why can't I restore the packages using the dotnet restore command?为什么我不能使用dotnet restore命令恢复包?

UPDATE:更新:

Packages are restored without errors when using the old Nuget Restore task as follows:使用Nuget 恢复任务时,包恢复没有错误,如下所示:

构建定义

UPDATE 2:更新 2:

I am able to restore the packages using the .NET Core task v1:我可以使用 .NET 核心任务 v1 恢复包:

屏幕截图 - 使用 .NET Core 任务 v1 恢复包

Or using v2 task with argument --force :或使用带有参数的 v2 任务--force

屏幕截图 - 使用 --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 时再次出现。

I had to change the nuget installer to 4.8.1 in order for this to work after switching VSTS url to the new Azure Devops url.我不得不将 nuget 安装程序更改为 4.8.1,以便在将 VSTS url 切换到新的 Azure Devops url 后使其工作。

在此处输入图片说明

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 包提要的步骤 -

  • In DevOps create a new PAT having the Packages scope as above在 DevOps 中创建一个具有上述 Packages 范围的新 PAT
  • In DevOps also get the package source URL from the Connect to feed page under Artifacts > Packages (this is required for the -source parameter for 'nuget sources add')在 DevOps 中,还可以从“工件”>“包”下的“连接到提要”页面获取包源 URL(这是“nuget 源添加”的 -source 参数所必需的)
  • 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 中解决相同问题的步骤。

  1. Make sure that you have Owner or Contributor permissions to the Feed in Azure DevOps.确保您对 Azure DevOps 中的提要拥有所有者或贡献者权限。

在此处输入图像描述

  1. Then in Visual Studio make sure that you are signed in using the account that has the permissions from the previous step.然后在 Visual Studio 中确保使用具有上一步权限的帐户登录。

在此处输入图像描述

  1. Finally rebuild the solution.最后重建解决方案。

Hopefully this fix your issue too!希望这也能解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM