如何通过 GitHub 操作在我的 dotnet 项目中使用 GPR 和 nuget.org 源?

How can I use GPR and nuget.org sources in my dotnet project via GitHub Actions?

我正在尝试通过 GitHub Actions 构建我的 dotnet class 库项目。除了一些标准的 nuget.org 包外,我的项目还使用存储在 GPR 上的私有 NuGet 包。我无法像在本地计算机上那样成功构建。

我正在使用 setup-dotnet(并尝试过 warrenbuckley/Setup-Nuget,但没有成功):

jobs:
  build:
    runs-on: windows-latest # I started with ubuntu-latest, but the dotnet nuget is missing functionality.
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.100'
          source-url: https://nuget.pkg.github.com/owner/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
      - run: dotnet restore --source # fails here
      - run: dotnet build -c Release --no-restore

我知道它找到了正确的 GPR,因为它在这里成功安装了我的私有包,但是找不到任何 nuget.org 包
我尝试过的另一种变体:

      - run: dotnet nuget add source https://nuget.pkg.github.com/myorg/index.json -n "My Source"
      - run: dotnet build -c Release --no-restore # fails here

在这里,它找不到我的私有包。
最近,我尝试了 nuget cli:

      - uses: warrenbuckley/Setup-Nuget@v1
      - run: nuget sources Add -Source https://nuget.pkg.github.com/myorg/index.json -Name "My Org" -username myorguser -password ${{secrets.GITHUB_TOKEN}} -StorePasswordInClearText
      - run: nuget restore # Fails here
      - run: dotnet build -c Release --no-restore

这提供了最多的信息,但仍然找不到我的 GPR 包,尽管(与前面的示例不同)它找到了我想要的所有来源expect (本地,nuget.org,和我的私人组织)。以下是一些看起来很有趣的日志:

32 NotFound https://nuget.pkg.github.com/myorg/download/mypackage/index.json
...
65 NotFound https://api.nuget.org/v3-flatcontainer/efinitycore/index.json
...
930     NU1101: Unable to find package MyPackage. No packages exist with this id in source(s): My Org, Microsoft Visual Studio Offline Packages, nuget.org
931 Errors in D:\a\WorkflowLibrary\WorkflowLibrary\WorkflowLibrary.Tests\WorkflowLibrary.Tests.csproj
932     NU1101: Unable to find package MyPackage. No packages exist with this id in source(s): My Org, Microsoft Visual Studio Offline Packages, nuget.org
...
935 NuGet Config files used:
    C:\Users\runneradmin\AppData\Roaming\NuGet\NuGet.Config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    C:\Program Files (x86)\NuGet\Config\Xamarin.Offline.config

942 Feeds used:
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
    https://api.nuget.org/v3/index.json
    https://nuget.pkg.github.com/myorg/index.json

这对我有用

职位: 构建:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- name: Setup Nuget.exe
  uses: nuget/setup-nuget@v1.0.2
- name: Add GPR Source using nuget.exe
  run: nuget sources add -name "GPR" -Source https://api.nuget.org/v3/index.json -Source https://nuget.pkg.github.com/{myorg}/index.json -Username {login} -Password ${{ token }}
- name: Setup .NET Core
  uses: actions/setup-dotnet@v1
  with:
    dotnet-version: 3.1.101
steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.100'
          source-url: https://nuget.pkg.github.com/owner/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

在上述步骤中,将 NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} 更改为 NUGET_AUTH_TOKEN: ${{secrets.YOUR_PAT}}。我遇到了同样的问题,我能够通过 PAT.

访问我的 GPR 私人提要

刚花了半天时间解决了和OP描述的问题类似的问题。必须注意跨存储库的情况,其中包 originate/are 在一个存储库中构建,然后试图通过另一个存储库中的 GH 操作恢复。在这种情况下,Actions 生成的 GITHUB_TOKEN 仅限于当前回购,并且无权查看来自 GH 组织内其他私人回购的包。这导致 Nuget 在尝试检查 GPR 中的包时显示 NotFound 响应代码,并最终显示 NU1101 错误代码。

有关潜在问题的更多信息:https://github.community/t/github-token-cannot-access-private-packages/16621/14

遗憾的解决方法是切换到使用个人访问令牌。