40

我正在尝试使用 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 }} 

日志输出:

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.

这是对应的 GitHub 问题(带有解决方法选项):https ://github.com/NuGet/Home/issues/8580

4

7 回答 7

36

第二次更新: 我在GitHub 问题中得到了答案jcansdale(尚未测试):

对 dotnet nuget push --api-key 选项的支持现已添加到 GitHub 包。出于某种原因,这始终有效,但是使用基本身份验证(nuget.config 文件中的密码)随机失败!

例子:

  - name: Publish Nuget to GitHub registry
    run: dotnet nuget push ./<project>/out/*.nupkg -k ${GITHUB_TOKEN} -s https://nuget.pkg.github.com/<organization>/index.json --skip-duplicate --no-symbols 
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

更新: 基于GitHub 上的 Dids 回答,我的配置现在如下所示:

name: NuGet Generation

on:
  push:
    branches:
      - master
  pull_request:
    types: [closed]
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-18.04
    name: Update NuGet package
    steps:

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

      - name: Setup .NET Core @ Latest
        uses: actions/setup-dotnet@v1
        with:
          source-url: https://nuget.pkg.github.com/<organization>/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}        
          
      - name: Build solution and generate NuGet package
        run: |  
          cd <project>
          dotnet pack -c Release -o out  

      - name: Push generated package to GitHub registry
        run: dotnet nuget push ./<project>/out/*.nupkg --skip-duplicate --no-symbols true

注意:在撰写本文时,我需要使用--no-symbols true而不是--no-symbols防止 dotnet NuGet 客户端出现异常。


老答案:

我切换到 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

于 2019-09-20T09:13:54.767 回答
7

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

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

笔记:

  • 这会为每个 git push 创建一个带日期戳的预发布版本并将其上传到 nuget
    • 为了使后缀起作用,您需要在.csproj<VersionPrefix>中设置而不是<Version>
    • 如果您不想要预发布后缀,请删除 --version-suffix 参数
  • shell 被显式设置为 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 问题的初始帖子),那么我们将不再需要此解决方法
于 2019-11-19T21:43:36.790 回答
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

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

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

于 2020-07-30T14:59:21.350 回答
3

确保您的项目文件具有以下内容

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <PackageId>Example.PackageName</PackageId>
    <Version>1.0.0</Version>
    <Authors>Author Engineering</Authors>
    <Company>Company Inc</Company>
    <PackageDescription>This package for ...!</PackageDescription>
    <RepositoryUrl>
https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl>
  </PropertyGroup>

这应该是用于构建、打包、发布和版本控制的 main.yaml:

name: Continuous Integration

on:
  push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Setup Dotnet Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.100
        source-url: https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
      env:
        NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

    - name: Setup Nuget Config
      run: sed 's/GITHUB_TOKEN/${{ secrets.GITHUB_TOKEN }}/g' .nuget.config > nuget.config

    - name: Build
      run: dotnet build --configuration Release

    - name: Version and Tag
      id: bump_version
      uses: mathieudutour/github-tag-action@v1
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

    - name: Prep Version String
      run: echo ::set-env name=VERSION_NUMBER::$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')

    - name: Define Package Name
      run: echo ::set-env name=PACKAGE_NAME::$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg"

    - name: Set Nuget Package Version
      uses: roryprimrose/set-vs-sdk-project-version@v1
      with:
        version: ${{ env.VERSION_NUMBER }}

    - name: Pack
      run: dotnet pack --configuration Release Example.PackageName

    - name: Publish Package
      run: dotnet nuget push Example.PackageName/bin/Release/*.nupkg --source https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json

    - name: Create Release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.bump_version.outputs.new_tag }}
        release_name: Release ${{ github.ref }}

于 2020-04-17T02:39:42.037 回答
1

我的工作解决方案:

  • 将 'usernamecompanyname' 替换为您的仓库的有效值
  • 我将构建和打包分开,以便在出现问题时更容易调试
  • 您可以ACTIONS_RUNNER_DEBUG在 github 机密中设置变量以true进行更详细的调试
  • 更改dotnet-version为您想要的 dotnet-sdk 版本
  • 无需GITHUB_TOKEN在您的 github repo secrests 中指定,此令牌默认存在

build_and_publish_nuget.yml:

name: Build and publish package

# Controls when the action will run. Triggers the workflow on push or pull request 
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@master

      - name: Setup .NET environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.102'
          source-url: https://nuget.pkg.github.com/usernamecompanyname/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

      - name: Build project
        run: dotnet build -c Release

      - name: Generate a NuGet package
        run: dotnet pack --no-build -c Release -o .

      - name: Push to GitHub package registry
        run: dotnet nuget push *.nupkg
于 2020-03-15T12:40:01.600 回答
0

GitHub 在将 NuGet 包发布到 GitHub 包时遇到了间歇性问题。我伸出援手,他们给了我两个选择。

选项 1:卷曲

curl -vX PUT -u "<username>:<TOKEN>" -F package=@PATH-TO-PKG-FILE.nupkg https://nuget.pkg.github.com/<OWNER>/

选项 2:DOTNET GPR 工具
https://github.com/jcansdale/gpr

dotnet tool install gpr -g
gpr push PATH-TO-PKG-FILE.nupkg -k <TOKEN>

我在 GitHub Action Workflow 中选择了选项 2 :

$file = Get-ChildItem -Path <where I output my nupkg file to> -Recurse -Filter *.nupkg | Select -First 1
gpr push $file.FullName -k ${{secrets.GITHUB_TOKEN}}          
于 2020-09-17T18:11:44.840 回答
0

其他答案太长了,我不知道为什么。这就是我所做的:

对于 NuGet.org:

- name: Push Package to NuGet.org
  run: dotnet nuget push *.nupkg -k ${{ secrets.NUGET_ORG_API_KEY }} -s https://api.nuget.org/v3/index.json

对于 GitHub.com:

- name: Push Package to GitHub.com
  run: dotnet nuget push *.nupkg -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/USERNAME/index.json
于 2022-01-07T20:08:10.947 回答