如何在GitHub操作中推送nuget包

jwi*_*mer 3 bash action github nuget github-actions

我正在尝试使用GitHub操作从我的项目中生成NuGet包,并将其推送到(私有)GitHub注册表。

我的脚本([NAME]字段已删除):

name: Update NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    name: Update NuGet 
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '2.2.105'
      - name: Package Release
        run: |  
          cd [SOLUTION_FOLDER]
          dotnet pack -c Release -o out
      - name: Publish Nuget to GitHub registry
        run: dotnet nuget push ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out/$(ls ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out) -s https://nuget.pkg.github.com/[USERNAME]/index.json -k ${GITHUB_TOKEN}  
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 
Run Code Online (Sandbox Code Playgroud)

日志输出:

info : Pushing [PROJECT_FOLDER].3.4.23.nupkg to 'https://nuget.pkg.github.com/[USERNAME]'...
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
error: An error occurred while sending the request.
error:   The server returned an invalid or unrecognized response.
##[error]Process completed with exit code 1.
Run Code Online (Sandbox Code Playgroud)

这是GitHub上最核心的问题(带有解决方法):https : //github.com/NuGet/Home/issues/8580

vle*_*lee 8

这是适用于所有平台的解决方法:

name: prerelease NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    # also works with windows-latest and macos-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v1
    - name: Build with dotnet
      run: dotnet build --configuration Release --version-suffix prerelease-$(date +%Y%m%d%H%M%S)
      shell: bash
    - name: Publish nuget
      run: |
           for f in ./[repository]/bin/Release/*.nupkg
           do
             curl -vX PUT -u "[user]:${{ secrets.GHPackagesToken }}" -F package=@$f https://nuget.pkg.github.com/[user]/
           done
      shell: bash
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 这会为每个 git push 创建一个带有日期戳的预发布版本并将其上传到 nuget
    • 要使后缀起作用,您需要在.csproj 中设置<VersionPrefix>而不是<Version>
    • 如果您不想要预发布后缀,请删除 --version-suffix 参数
  • 外壳被明确设置为 bash 以允许与在 Windows 上构建的兼容性
  • 您需要用您自己的特定值替换上面的 [user] 和 [repository]
    • 您将需要创建一个具有 write:packages 权限的个人访问令牌
    • 然后创建一个名为 GHPackagesToken的GitHub Secret并将上面创建的令牌放在那里
    • 使用 GitHub Secrets 消除了对包含令牌的单独文件的需要
  • 这假设您<GeneratePackageOnBuild>true</GeneratePackageOnBuild>在 .csproj 中有
    • 如果你不这样做,那么你将需要一个额外的步骤运行 dotnet pack
  • 确保<RepositoryUrl>...</RepositoryUrl>在您的 .csproj 中指定
  • 对于一个工作示例,如果您无法使上述代码工作,请参阅https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml,它推送到https://github。 com/vslee/IEXSharp/packages(忽略我在那里的所有无关评论)
    • 我发布了这个 bc 我尝试了上面 jwillmer 的两个示例,以及 GH 问题线程上的 @anangaur 和 @marza91,但都不适合我(在任何平台上)
  • 一旦 GitHub 修复了无法直接在dotnet nuget push命令中使用 API 密钥的问题(请参阅GH 问题的初始帖子),那么我们将不再需要此解决方法


jwi*_*mer 5

我切换到Windows映像,并根据@anangaur的示例使其工作。这是我的最终代码:

name: NuGet Generation

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: windows-latest
    name: Update NuGet 
    steps:

      - name: Checkout repository
        uses: actions/checkout@master

#  latest image has .NET already installed!
#      - name: Setup .NET environment
#        uses: actions/setup-dotnet@v1
#        with:
#          dotnet-version: '2.2.105' 

      - name: Build solution and generate NuGet package
        run: |  
          cd SOLUTION_FOLDER
          dotnet pack -c Release -o out  

      - name: Install NuGet client
        uses: warrenbuckley/Setup-Nuget@v1

      - name: Add private GitHub registry to NuGet
        run: nuget sources add -name "GPR" -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GITHUB_TOKEN }}

      - name: Push generated package to GitHub registry
        run: nuget push .\SOLUTION_FOLDER\PROJECT_FOLDER\out\*.nupkg -Source "GPR" -SkipDuplicate

Run Code Online (Sandbox Code Playgroud)

  • 确保在“csproj”文件中设置“RepositoryUrl”值 (2认同)

Dan*_*iel 5

您可以使用以下dotnet nuget add source命令

    - name: NuGet push
      run: |
        dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json --name github --username ${{ github.repository_owner }} --password ${{ github.token }} --store-password-in-clear-text
        dotnet nuget push **/*.nupkg --source github
Run Code Online (Sandbox Code Playgroud)

--store-password-in-clear-text在 linux 环境中运行时,我需要该选项。

使用此方法,无需修改actions/setup-dotnet任务。此外,如果需要,此方法将允许您推送到多个 NuGet 流。