如何在GitHub操作中推送nuget包

时间:2019-09-11 12:56:29

标签: bash github nuget action 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 }} 

日志输出:

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上最核心的问题(带有解决方法):here

6 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:4)

这是在所有平台上都可以使用的解决方法:

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]
    • 您将需要创建一个personal access token,并具有write:packages权限
    • 然后创建一个名为GHPackagesToken的GitHub Secret并将上面创建的令牌放在其中
    • 使用GitHub Secrets消除了对包含令牌的单独文件的需求
  • 这假设您的.csproj中有<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    • 如果没有,那么您将需要执行dotnet pack
  • 确保在.csproj中指定<RepositoryUrl>...</RepositoryUrl>
  • 一个有效的示例,如果您无法使以上代码正常工作,请参阅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命令(请参阅initial post of GH issue)中使用API​​密钥的问题,那么我们将不再需要这种解决方法

答案 2 :(得分: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 }}

答案 3 :(得分:2)

我的工作解决方案:

  • 用回购的有效值替换“ usernamecompanyname”
  • 我将构建和打包分开放置,以便在出现问题时更容易进行调试
  • 您可以将github机密中的ACTIONS_RUNNER_DEBUG变量设置为true,以进行更详细的调试
  • dotnet-version更改为所需的dotnet-sdk版本
  • 无需在您的github回购协议中指定GITHUB_TOKEN,默认情况下会显示此令牌

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

答案 4 :(得分:2)

您可以使用dotnet nuget add source command

    - 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

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

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

答案 5 :(得分:1)

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

选项1:CURL

import json

json.loads(json_str)

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

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

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

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