将 NuGet 包推送到 GitHub 时,Azure DevOps 管道失败并出现 401 未经授权错误

问题描述 投票:0回答:1

我遇到了 Azure DevOps 管道的问题,该管道应该将 NuGet 包推送到 GitHub Packages。该管道有两个主要任务:NuGetAuthenticateNuGetCommandNuGetAuthenticate 任务似乎成功,但 NuGetCommand 任务失败并出现 401 未经授权错误:

##[error]The nuget command failed with exit code(1) and error(Response status code does not indicate success: 401 (Unauthorized).

我已确认使用的个人访问令牌 (PAT) 是正确的,并且具有必要的范围,因为我可以从 PowerShell 手动执行 dotnet nuget Push 将包推送到 GH。

这是管道相关部分的片段:

- ${{ if parameters.gitHubNuGetExternalFeed }}: 
  - task: NuGetAuthenticate@0
    displayName: 'NuGet Authenticate'
    inputs:
      nuGetServiceConnections: '${{ parameters.gitHubNuGetExternalFeed }}'
    enabled: true
    continueOnError: true

  - task: NuGetCommand@2
    displayName: Publish to GitHub Packages Nuget Feed
    inputs:
      command: 'push'
      nuGetFeedType: 'external'
      publishFeedCredentials: '${{ parameters.gitHubNuGetExternalFeed }}'
      packagesToPush: '$(Common.TestResultsDirectory)/NuGet/*.*nupkg'
      versioningScheme: 'byEnvVar'
      versionEnvVar: $(GitVersion.SemVer)
      includeSymbols: true
      buildProperties: 'description=test'
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
    enabled: true
    continueOnError: true

我不确定为什么 NuGetCommand 失败并出现 401 错误,因为 PAT 已被确认可以工作并且 NuGetAuthenticate 任务成功。任何有关如何解决此问题的见解或建议将不胜感激。

我最初使用 ApiKey 方法设置了与 NuGet feed 的服务连接。我的 Feed URL 是 https://nuget.pkg.github.com/myorganization/index.json,对于 ApiKey,我使用了在我的组织中创建的完全访问 PAT,我是该组织的所有者。同样的 PAT 之前曾在 PowerShell 自动化中用于手动下载 NuGet 包并将其从 Azure Artifacts 推送到 GitHub 包,并且它在该上下文中经过身份验证并运行良好。

最初,我的管道只有 ** NuGetCommand** 任务,该任务在我的服务连接中抛出 401 错误。为了解决这个问题,我添加了一个 NuGetAuthenticate 步骤。然而,这导致了一个错误:

##[error]Error: The service connection for 'https://nuget.pkg.github.com/myorganization/index.json' is not valid. ApiKey service connections are not supported in this task. Instead, use -ApiKey (NuGet) or --api-key (dotnet) when invoking the tool itself. See the task documentation for more details.

因此,我使用我的个人用户名和 PAT 令牌将服务连接更改为基本身份验证。此更改允许 NuGetAuthenticate 步骤成功,但 NuGetCommand 步骤中的 401 错误仍然存在。

我期望通过正确配置服务连接并确保 PAT 具有所需的范围,管道将验证并推送包而不会出现任何问题。然而,NuGetCommand 步骤中持续存在的 401 错误令人费解,特别是考虑到 NuGetAuthenticate 步骤现在已成功。任何有关可能导致此问题的原因或我可以采取进一步措施来解决此问题的见解将不胜感激。

azure-devops yaml nuget cicd github-packages
1个回答
0
投票

使用 NuGetCommand@2

 任务推送到 GitHub NuGet Packages 注册表时,我可以重现相同的 
401 错误。

但是,切换到使用

DotNetCoreCLI@2
任务推送到 GitHub NuGet Packages 注册表,它可以正常工作。您还可以尝试在管道中使用
DotNetCoreCLI@2
任务。

© www.soinside.com 2019 - 2024. All rights reserved.