Azure Pipelines 托管代理无法访问 DevOps 项目提要

Azure Pipelines Hosted Agent Can't Access DevOps project Feed

我有一个 Azure DevOps 项目(只有一个)。

我在 "Hosted VS2017" 代理池中将构建管道设置为 运行。此代理池似乎属于 [MyProject]\Build Administrators、Contributors、Project Administrators 和 Release Administrators 角色。

我在 DevOps 项目中也有一个 Artifacts nuget 提要。它具有 [MyProject]\Project Valid Users 设置为 "Reader" 角色。 Project Valid Users 似乎具有上述代理池的所有角色作为成员。

我有一个 azure-pipelines.yml 脚本,它在开头添加了工件提要作为 nuget 源:

# Add nuget source
- powershell: Invoke-RestMethod "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile "$env:UserProfile/nuget.exe"
- script: '%UserProfile%\nuget.exe sources Add -Name "devops" -Source "https://pkgs.dev.azure.com/MyProject/_packaging/feed/nuget/v3/index.json"'

构建 yml 然后点 dotnet build 但在 NuGet.targets 内失败:

Unable to load the service index for source https://pkgs.dev.azure.com/MyProject/_packaging/feed/nuget/v3/index.json.
Response status code does not indicate success: 401 (Unauthorized).

我怎样才能完成这项工作?我的构建需要来自该工件提要上的其他构建的包...

使用 built-in tasks 安装和 运行 NuGet,您将不会遇到身份验证问题。

使用dotnet任务的恢复命令。如果您使用的是单个 Azure Artifacts 源,只需 select 从任务的下拉列表中获取它(而不是您提到的 PowerShell)。如果有多个提要(从您的问题看不像,但以防万一),您需要签入引用所有这些提要的 NuGet.config,然后将任务指向该配置。

您可能还需要将“--no-restore”标志传递给您的 'dotnet build' 命令。

如果您仍然遇到问题,请确保 correct build identity 可以访问您的 Feed。

我有更好的选择。您可以继续使用您的脚本 dotnet restore。 只需在 NuGetAuthenticate@0

之前添加一个任务
steps:
- task: NuGetAuthenticate@0
- script: dotnet restore --no-cache --force

此任务将使用需要的 nuget 源验证管道,并在 NuGet.config 文件中找到。

More info here

请注意,当 nuGet 源在 Azure DevOps 中时,不需要其他任何东西。如果提要是外部的,您可以在 Azure DevOps 上配置 nuGet 服务连接(在 link 处有更多信息)。