访问 GitHub Packages 时 GitHub Actions 上的 dotnet Restore 问题

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

我面临涉及 dotnet 恢复的 GitHub Actions 工作流程问题。该存储库托管在 GitHub 上,我正在尝试从 GitHub 包源恢复 NuGet 包。

这是我的 GitHub Actions 工作流程 YAML 的片段:

name: Build .NET 7 + Pack + Push NuGet

on:
  workflow_dispatch: # Allow running the workflow manually from the GitHub UI
  push:
    branches:
      - "main" # Run the workflow when pushing to the main branch
  pull_request:
    branches:
      - "main" # Run the workflow for all pull requests
  release:
    types:
      - published # Run the workflow when a new GitHub release is published

env:
  DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
  DOTNET_NOLOGO: true
  NuGetDirectory: ${{ github.workspace }}/out

defaults:
  run:
    shell: pwsh

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
      packages: read

    steps:
      - uses: actions/labeler@v3
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN}}
    
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Get all history to allow automatic versioning using MinVer

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x

      - name: Add GitHub NuGet Source
        id: add-nuget-source
        run: |
          dotnet nuget add source https://nuget.pkg.github.com/user/index.json -n github -u user -p ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text
        continue-on-error: true

      - name: Restaurando Dependências
        run: dotnet restore
        #if: success() && steps.add-nuget-source.outputs.result == '0'

      - name: Build do Projeto
        run: dotnet build -c Release -o out
        
      #- name: Test
      #  run: dotnet test --no-build --verbosity normal

      # Publish the NuGet package as an artifact, so they can be used in the following jobs
      - uses: actions/upload-artifact@v3
        with:
          name: nuget
          if-no-files-found: error
          retention-days: 7
          path: ${{ env.NuGetDirectory }}/*.nupkg

  run_test:
    name: Rodando os Testes Unitários
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
      - name: Run tests
        run: dotnet test --configuration Release

  deploy:
    # Publish only when creating a GitHub Release
    # https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository
    # You can update this logic if you want to manage releases differently
    # if: github.event_name == 'release'
    runs-on: ubuntu-latest
    needs: [run_test]
    # Download the NuGet package created in the previous job
    steps:
      - uses: actions/download-artifact@v3
        with:
          name: nuget
          path: ${{ env.NuGetDirectory }}

      - name: Publicando os pacotes Nuget
        run: |
          cd out
          dotnet nuget push "*.nupkg" --api-key "${{ secrets.NUGET_APIKEY }}" --source "${{ secrets.NUGET_SERVER }}" --skip-duplicate

该问题发生在 dotnet 恢复步骤期间,并出现以下错误:

/usr/share/dotnet/sdk/8.0.100/NuGet.targets(156,5):警告:GitHub Packages 服务无法对您的请求进行身份验证。请确保您的访问令牌有效并且配置了适当的范围。 正在重试源“https://nuget.pkg.github.com/user/download/package/index.json”的“FindPackagesByIdAsync”。

附加信息:

NuGet 包存储在 GitHub 存储库中,我已将工作流程配置为使用 GitHub 令牌进行身份验证。 该问题似乎与 dotnet 恢复步骤期间的身份验证有关。 如果您能提供有关如何正确配置 GitHub Actions 工作流程以解决此问题的指导,我将不胜感激。 谢谢您的协助!

.net authentication nuget github-actions github-packages
1个回答
0
投票

我收到了完全相同的错误消息,并设法通过使用具有

ClearTextPassword
权限的活动令牌更新 nuget.config 中 github 的
packageSourceCredentials
中的
read:packages
来解决该问题。

<packageSourceCredentials>
    <github>
        <add key="Username" value="YourUser" />
        <add key="ClearTextPassword" value="{yourTokenWithPackageReadPermissions}" />
    </github>
</packageSourceCredentials>
© www.soinside.com 2019 - 2024. All rights reserved.