[英]Nuget restore returns 401 unauthorized during Jenkins build for packages in Azure DevOps artifacts feed
我们有一个 CI 构建在 Jenkins 中工作。 We have a NuGet package feed in Azure DevOps, and we are getting a 401 Unauthorized
error when restoring NuGet packages during a build:
https://pkgs.dev.azure.com/X/_packaging/FeedName@Release/nuget/v3/index.json : Unable to load the service index for source https://pkgs.dev.azure.com/X/ _packaging/FeedName@Release/nuget/v3/index.json 。 响应状态码不表示成功:401(未授权)。
Jenkins 设置:
我在 Jenkins 机器上安装了 NuGet 凭据提供程序,但是我在以自己的身份登录时安装了它。 我复制了我在笔记本电脑上采取的步骤来解决同样的问题。 我的笔记本电脑正在工作。 Jenkins 服务器不是,我认为它与 NuGet 凭据提供程序不适用于“本地系统”帐户有关,但不知道如何解决此问题。
构建由 PowerShell 脚本控制,NuGet 恢复命令如下所示:
& "$solutionPath"\.nuget\NuGet.exe restore "$solutionFile"
$solutionPath
是 Visual Studio 的 .sln 文件所在的文件夹$solutionFile
是 .sln 文件的确切文件名在阅读如何在 VSTS nuget 恢复中为外部 nuget 提要指定凭据后,我还通过以下方式向本地 NuGet 安装添加了一个新的“源”:
.\.nuget\NuGet.exe sources add -name "FeedName" -source "https://pkgs.dev.azure.com/X/_packaging/FeedName@Release/nuget/v3/index.json" -username <my username> -password <my personal access token> -configfile .\.nuget\NuGet.config
我试过的用户名:
.
, 就像 Jenkins 配置需要通过 TFVC 获取最新版本的代码时使用 Azure DevOps 进行身份验证 -password
参数是对 Azure DevOps 工件提要具有读/写权限的个人访问令牌的复制和粘贴。
但同样,当我这样做时,我被远程访问了 Jenkins 机器作为我的用户名,我仍然遇到同样的问题。
How to fix 401 Unauthorized
when restoring NuGet packages during a Jenkins build when the packages are coming from an Azure DevOps artifacts NuGet feed?
尝试使用个人访问令牌 (PAT) 的名称作为用户名。 这是您在创建 PAT 时为其指定的名称。
编辑.nuget/NuGet.config
并添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="MyPackages" value="https://pkgs.dev.azure.com/OrganizationName/_packaging/FeedName@Release/nuget/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<!-- Tag name 'MyPackages' matches 'key' attribute under packageSources -->
<MyPackages>
<!-- Username is the name you gave your Personal Access Token in Azure -->
<add key="Username" value="NameOfPersonalAccessToken" />
<!-- Choose one of the tags below: -->
<!-- Use Password if PAT is encrypted -->
<add key="Password" value="EncryptedPersonalAccessTokenGoesHere" />
<!-- Use ClearTextPassword if PAT is encrypted -->
<add key="ClearTextPassword" value="PlainTextPersonalAccessTokenGoesHere" />
</MyPackages>
</packageSourceCredentials>
</configuration>
更多信息: https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials
虽然 OP 未被阻止,但我遇到了一个完全不同的问题,导致使用托管在 ADO 上的私有 NuGet 提要在 Azure DevOps 管道上出现相同的行为。
我遵循Azure Artifacts Credential Provider guide ,并将VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
的环境变量设置为 Secret 变量。
经过几天令人毛骨悚然的调试后,我遇到了关于GitHub 问题的评论,这让我大吃一惊。 在适当的情况下,该 env-var 会自动生成。
我应该在我的第一条消息中提到这一点,但是如果 Azure Artifacts credential provider (V2) 正在使用并且“Credentials”部分/colle 在此帐户之外的提要填充,则 NuGet 构建任务将自动填充 VSS_NUGET_EXTERNAL_FEED_ENDPOINTS 变量
所以我按照 rest 的说明设置了这些变量,即使已经设置了 SESSIONTOKENCACHE。
NuGet.ForceEnableCredentialProvider=false
NuGet_ForceEnableCredentialProviderV2=true
NUGET_CREDENTIALPROVIDER_MSAL_ENABLED=true
NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED=true
并完全删除VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
。
就是这样。 终于解封了
奇怪的是,在我被解除阻止之前, NuGet 可以正常进行restore
和push
,只是没有list
......非常奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.